簡體   English   中英

如何從 Xamarin 項目中的跨平台頁面打開和關閉 Android 活動

[英]How to open and close Android activity from cross-platform page in Xamarin project

我有跨平台應用程序,但我的應用程序中的一個 function 只能在 Android (GooglePay) 中使用。 我為它創建了一個活動,並嘗試使用 DependencyService 在 Xamarin 表單頁面中打開此活動:

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

我的活動代碼在這個答案

因此,據我了解,我需要使用 Bundle object 來調用它,因為我在這段代碼中遇到了異常:

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

據我了解,活動是 null 沒有捆綁。 例外是:

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

所以,我找不到,如果它是打開活動的正確方法,以及,如果它是正確的,如何創建正確的 Bundle。

Google 提供了 SupportWalletFragment class,它將在片段中顯示一個品牌購買按鈕:

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 class 用於構建新的購買請求。 您可以使用任何可以接受 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 文檔

好吧,終於,我找到了解決這個問題的方法。

使用了這個源 - 不是功能性的,僅適用於 GPay India,但有一些優點:

  • 使用 MessagingCenter 將數據發送到本機代碼並取回
  • 使用 MainActivity,因為它是唯一可以與 'null' Bundle 一起使用的活動

所以現在我的代碼:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM