c# - 如何在 Xamarin 中获取 GoogleApiClient 实例?

标签 c# android xamarin xamarin.android

我想将我的 xamarin Android 应用程序与 Google Play 服务排行榜和成就相集成。我不知道如何从 android documentation 转换以下代码到 c#。

   // Create the Google Api Client with access to the Play Game and Drive services.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER) // Drive API
            .build();

    // ...

我尝试转换如下内容

GoogleApiClient api = new GoogleApiClient.Builder(this)
           .AddApi(Android.Gms.Games.API)
           .Build();

在“Android.Gms.Games.API”下出现错误

this 中没有提到任何事情堆栈溢出线程正在工作。看起来这些东西中的大部分都已弃用。

请建议是否有任何其他可与排行榜集成的简单方法。

编辑:进行了更改,现在出现以下错误。

enter image description here

最佳答案

您将能够通过以下方式访问 GoogleClientAPI:

var googleClientAPI = new GoogleApiClient.Builder(Application.Context).AddApi(XXX).Build();

使用阻塞连接作为快速测试的示例:

var client = new GoogleApiClient.Builder(Application.Context)
                                .AddApi(GamesClass.API)
                                .AddScope(GamesClass.ScopeGames)
                                .Build();
Task.Run(() => {
    client.BlockingConnect();
    System.Diagnostics.Debug.WriteLine(client.IsConnected);
});

注意:这假设您已经注册了您的应用程序,否则您将收到致命的开发人员错误...参见Play Connecting


由于您需要访问排行榜和成就,请确保您已添加包:

  • Xamarin.GooglePlayServices.Games
  • Xamarin.GooglePlayServices.Identity

这些将自动包含 .Base.Basement


  • 添加包后要查看的命名空间:

using Android.Gms.Common.Apis;
using Android.Gms.Games.LeaderBoard;
using Android.Gms.Games.Achievement;

关于c# - 如何在 Xamarin 中获取 GoogleApiClient 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233100/

相关文章:

xamarin.ios - 如何在 Visual Studio 中更改 Xamarin 项目的 .NET 框架

c# - 将集合项目设置为只读

c# - 不继承 IEnumerable 扩展方法的列表

安卓 DexGuard : Exclude Crashlytics from Obfuscation

android - SearchView.onQueryTextListener 在 Fragment (Kotlin) 中不起作用

c# - Xamarin 依赖绑定(bind)

c# - 如何将下载的文件保存到本地文件夹?

c# - MVVM的良好做法是在转换器中初始化StackPanel并使用ContentPresenter呈现吗?

android - 更改墙纸 Intent 应用程序在 Nook Color : 中崩溃

android - 如何从 App Center Push SDK 的用户 ID 中删除用户 ID 和/或设备 ID/信息