[英]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,但有一些优点:
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.