【问题标题】:How to get GoogleApiClient instance in Xamarin?如何在 Xamarin 中获取 GoogleApiClient 实例?
【发布时间】:2016-05-15 09:46:50
【问题描述】:

我想将我的 xamarin android 应用与 google play services 排行榜和成就集成。我不知道如何将下面的代码从 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 堆栈溢出线程中提到的所有事情都不起作用。看起来大部分这些东西都已弃用。

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

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

【问题讨论】:

    标签: c# android xamarin xamarin.android


    【解决方案1】:

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

    【讨论】:

    • 酷。看起来我已经添加了所有引用。我没有遵循您提到的语法。我现在很好。谢谢...
    • 我按照你说的做了修改。现在它给出了错误。在问题中查看我的编辑。
    • @RajKarri 我添加了一个示例,请确保您已注册您的应用程序(见注释)
    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2019-07-08
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多