简体   繁体   English

如何从 Xamarin 项目中的跨平台页面打开和关闭 Android 活动

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

I have cross-platform app, but one function in my app can work only in Android (GooglePay).我有跨平台应用程序,但我的应用程序中的一个 function 只能在 Android (GooglePay) 中使用。 I created an activity for it, and trying to open this activity in Xamarin Form page using DependencyService:我为它创建了一个活动,并尝试使用 DependencyService 在 Xamarin 表单页面中打开此活动:

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

Code of my activity is in this answer我的活动代码在这个答案

So, as I understand, I need to call it with Bundle object, because I got an exception in this code:因此,据我了解,我需要使用 Bundle object 来调用它,因为我在这段代码中遇到了异常:

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

As I understand, activity is null without a bundle.据我了解,活动是 null 没有捆绑。 Exception is:例外是:

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

So, I cannot find, if it is the right way to open an activity, and, if it is right, how to create right Bundle.所以,我找不到,如果它是打开活动的正确方法,以及,如果它是正确的,如何创建正确的 Bundle。

Google provides the SupportWalletFragment class, which will display a branded Purchase button within the fragment: 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 ());

The MaskedWalletRequest class is used to build a new purchase request. MaskedWalletRequest class 用于构建新的购买请求。 You could use any payment gateway that can accept EMVCO Network Tokens, or in this case set up your Stripe account as the payment gateway with some configuration options:您可以使用任何可以接受 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();

For more details you could check the devblogs .有关更多详细信息,您可以查看devblogs

You could also use the plugin InAppBillingPlugin from Nuget.您还可以使用来自 Nuget 的插件InAppBillingPlugin And get started by reading through the In-App Billing Plugin documentation .并开始阅读In-App Billing Plugin 文档

Well, finally, I found the way to solve this problem.好吧,终于,我找到了解决这个问题的方法。

Used this sourse - not funcional, works only with GPay India, but have some good points:使用了这个源 - 不是功能性的,仅适用于 GPay India,但有一些优点:

  • Use MessagingCenter to send data to native code and get it back使用 MessagingCenter 将数据发送到本机代码并取回
  • Use MainActivity, because it is only activity, that can work with 'null' Bundle使用 MainActivity,因为它是唯一可以与 'null' Bundle 一起使用的活动

So now my code:所以现在我的代码:

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
}

And code in MainActivity: 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.

相关问题 Xamarin android项目和Xamarin跨平台项目有什么区别? - What is the difference between Xamarin android project and Xamarin cross-platform project? 如何重命名Xamarin跨平台应用程序? - How to rename a Xamarin Cross-Platform App? Xamarin如何从android项目打开xamarin表单页面? - Xamarin how to open xamarin forms page from android project? 如何在Xamarin Forms跨平台中绑定图像集合 - How to bind collection of images in Xamarin Forms Cross-Platform 如何从 Xamarin Forms 在 a.xaml 文件中获取跨平台应用程序中的可用屏幕尺寸值? - How to Get The Avaible Screen Size Value In Cross-Platform App From Xamarin Forms in a .xaml File? Xamarin中的跨平台OpenGL渲染 - Cross-platform OpenGL rendering in Xamarin 用于跨平台Xamarin Forms的UserControl - UserControl for cross-platform Xamarin Forms 创建Xamarin.Forms跨平台应用程序时出现“此项目需要Visual Studio更新才能加载”错误 - “This project requires a Visual Studio update to load” error when creating a Xamarin.Forms Cross-Platform Application 跨平台渲染器的正确项目类型 - Proper Project Type for Cross-platform Renderer 创建Xamarin.Forms跨平台应用程序时,“此项目需要加载Visual Studio更新”错误 - “This project requires a Visual Studio update to load” error when creating a Xamarin.Forms Cross-Platform Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM