Xamarin Forms 在适用于 Android 的应用计费中



我正在制作一个应用程序,我必须在应用程序中购买(购买我可以在应用程序中进一步使用的密钥)。

我已经 http://components.xamarin.com/view/xamarin.inappbilling 看过这个组件,但我不知道如何在 xamarin 形式中实现它。有人愿意帮助我解决这个问题吗?是否有任何带有应用内购买的开源项目可供我查看?

我知道

晚了,但这可能会对某人有所帮助:

实现此目的的方法是创建一个服务,然后将其呈现到将在表单项目中使用的标准接口(根据您的要求)。

您甚至可以使用 MessagingCenter 在 Android 和 Xamarin.Forms 项目之间进行通信。

FormsPrject:
MessagingCenter.Send<MainPage, string>(this, "BuyProduct", "buyButtonPressed");
AndroidPoject
MessagingCenter.Subscribe<MainPage, string>(this, "BuyProduct", (sender, arg) =>
{
    //logic to buy product
}

希望有帮助!!

这个问题非常模糊,所以我将提供一个一般性的答案和一些我认为重要的注释。 我正在使用Visual Studio 2015和Xamarin Forms 2.3.0.107。

我会为此使用抽象,而不是直接在项目之间发送消息。

基本思想是,您将在 Xamarin 窗体项目中创建一个公共接口。 由于 Andriod 项目具有对 Xamarin Forms 项目的引用,因此它可以使用此公共接口。 然后,您将在您的Android项目中实现此接口的所有计费逻辑。 在 Xamarin Forms 项目中。 使用依赖项服务,我们可以将实现的现有实例获取到 Xamarin 窗体项目中。 然后,您可以针对接口进行编码。 如果您想执行 iPhone 或其他实现,这将特别有用,因为您永远不需要更改 Xamarin 窗体代码;您可以插入新的实现。

  • 这可能有点超出范围,但请务必满足 Google 的所有要求,包括设置您的开发者帐户、商家帐户和 API 帐户。 这一切都非常混乱和混乱。
  • nuget 中的 Xamarin 组件当前版本为 1.5。 但是,该组件具有较新的已发布版本。 您想要使用较新的(2.0 或更高版本)版本。
  • 使用 Android SDK 管理器安装 Google Play 计费库
  • 在 Android 项目中,添加对 Xamarin.InAppBilling
  • 的引用,并添加 Xamarin.InAppBilling 组件。
  • Google 对象必须存在于 Android 活动中,因为您依赖于覆盖活动方法来完成购买(我在这里使用了 MainActive,并将 google 对象设置为静态对象以便于访问)
  • 用Google Play测试这一点很麻烦。 由于版本之间的差异,文档令人困惑。 在发布应用之前,不能使用实际的产品 ID。 它们提供可在测试期间使用的测试 ID,但它们仅提供某些功能。

我使这些代码示例尽可能少,以说明这个概念。 你显然会想做更多的事情。

Xamarin 窗体项目

创建接口:

public interface IInAppBilling
{
    void Pay(string productId);
}

每当您想使用结算服务时,都可以使用 IInAppBilling billingService = DependencyService.Get<IInAppBilling>(); 来获取对特定于设备 (Android) 实现的引用。

//call this from a button click or whatever
void BuySomething(string somethingId)
{
    //Get any IInAppBilling object that is registered with the DependencyService.
    IInAppBilling billingService = DependencyService.Get<IInAppBilling>();
    billingService.Pay(somethingId);
}

安卓项目

重写活动的 OnCreate 方法并创建 InAppBillingServiceConnection:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    public static InAppBillingServiceConnection google;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        google = new InAppBillingServiceConnection(this, "MII...ApplicationKey");
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

创建一个实现我们之前创建的 Xamarin 窗体接口的类。 重要的是不要忽略顶部的assembly: Dependency注释。 这就是使类可用于 Xamarin 窗体对象中的依赖项服务的原因:

[assembly: Dependency(typeof(com.myapp.InAppBilling))]
namespace com.myapp
{
    class InAppBilling :IInAppBilling
    {
        public void Pay(string productId)
        {
             MainActivity.google.BillingHandler.BuyProduct(productId, ItemType.Product, "MyUniquePayload");
        }
    }
}

重写活动的 OnActivityResult 方法以完成购买:

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        // Ask the open service connection's billing handler to process this request
        try
        {
            google.BillingHandler.HandleActivityResult(requestCode, resultCode, data);
        }
        catch (Exception ex)
        {
            //log it or something?
        }
    }

最新更新