【问题标题】:How to open and close Android activity from cross-platform page in Xamarin project如何从 Xamarin 项目中的跨平台页面打开和关闭 Android 活动
【发布时间】:2021-02-10 16:53:22
【问题描述】:

我有跨平台应用,但我的应用中的一项功能只能在 Android (GooglePay) 中使用。我为它创建了一个活动,并尝试使用 DependencyService 在 Xamarin 表单页面中打开此活动:

IPay pay = DependencyService.Get<IPay>();
Console.WriteLine(pay.GetResult());

我的活动代码在this answer

所以,据我了解,我需要用 Bundle 对象调用它,因为我在这段代码中遇到了异常:

PaymentsClient paymentsClient = WalletClass.GetPaymentsClient(
             this,
             new WalletClass.WalletOptions.Builder()
                     .SetEnvironment(WalletConstants.EnvironmentTest)
                     .Build()
        );

据我了解,没有捆绑的活动是空的。例外是:

Java.Lang.NullPointerException. Message = Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

所以,我找不到打开活动的正确方法,如果正确,如何创建正确的 Bundle。

【问题讨论】:

  • 如果回复有帮助,请采纳为答案(点击该答案左上角的“✔”),对有类似问题的其他人有帮助

标签: c# android xamarin


【解决方案1】:

Google 提供了 SupportWalletFragment 类,它将在片段中显示一个品牌购买按钮:

var walletFragment = SupportWalletFragment.NewInstance (WalletFragmentOptions.NewBuilder ()
    .SetEnvironment (WalletConstants.EnvironmentSandbox)
    .SetMode (WalletFragmentMode.BuyButton)
    .SetTheme (WalletConstants.ThemeLight)
    .SetFragmentStyle (new WalletFragmentStyle ()
        .SetBuyButtonText (BuyButtonText.BuyWithGoogle)
        .SetBuyButtonAppearance (BuyButtonAppearance.Classic)
        .SetBuyButtonWidth (Dimension.MatchParent))
    .Build ());

MaskedWalletRequest 类用于构建新的采购请求。您可以使用任何可以接受 EMVCO 网络令牌的支付网关,或者在这种情况下将您的 Stripe 帐户设置为带有一些配置选项的支付网关:

var maskedWalletRequest = MaskedWalletRequest.NewBuilder ()

// Request credit card tokenization with Stripe
.SetPaymentMethodTokenizationParameters (
    PaymentMethodTokenizationParameters.NewBuilder ()
        .SetPaymentMethodTokenizationType (PaymentMethodTokenizationType.PaymentGateway)
        .AddParameter ("gateway", "stripe")
        .AddParameter ("stripe:publishableKey", STRIPE_PUBLISHABLE_KEY)
        .AddParameter ("stripe:version", "1.15.1")
        .Build ())
    .SetShippingAddressRequired (true)
    .SetMerchantName ("Xamarin")
    .SetPhoneNumberRequired (true)
    .SetShippingAddressRequired (true)
    .SetEstimatedTotalPrice ("20.00")
    .SetCurrencyCode ("USD")
    .Build();

更多详情请查看devblogs

您也可以使用来自 Nuget 的插件 InAppBillingPlugin。并开始阅读In-App Billing Plugin documentation

【讨论】:

  • 问题是我不能使用 Stripe,因为我的国家不支持它,以及带有 EMVCO 网络令牌的网关。
【解决方案2】:

嗯,终于,我找到了解决这个问题的方法。

使用thissourse - 没有功能,仅适用于 GPay India,但有一些优点:

  • 使用 MessagingCenter 将数据发送到本机代码并取回
  • 使用 MainActivity,因为它是唯一可以使用的活动 'null' 捆绑包

现在我的代码:

long orderId = 1;
long sum = 1;
long[] data = new long[2] {orderId, sum };

MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "PayViaGooglePay", data);

MessagingCenter.Subscribe<App, string>((App)Application.Current, $"tokenteleportation{orderId}", async (sender, arg) =>
        {
// getting result from arg
}

MainActivity 中的代码:

MessagingCenter.Subscribe<App, long[]>((App)Xamarin.Forms.Application.Current, "PayViaGooglePay", (sender, arg) =>
        {
            OrderId = arg[0];
            PayViaGooglePay(this, arg[1]);
        });

//sending request, getting token

MessagingCenter.Send((App)Xamarin.Forms.Application.Current, $"tokenteleportation{OrderId}", TOKEN);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多