如何在 Xamarin 中获取 GoogleApiClient 实例?

How to get GoogleApiClient instance in Xamarin?

我想将我的 xamarin android 应用与 google 播放服务排行榜和成就集成。我不知道如何将下面的代码从 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"

下报错

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

如果有任何其他可与排行榜集成的简单方法,请提出建议。

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

您将能够通过以下方式访问 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;