【发布时间】:2018-09-24 02:43:14
【问题描述】:
我正在尝试使用plugin InAppBilling 为 Xamarin 表单解决方案实施 (应用程序计费)
除了@987654322,我找不到此插件的任何完整代码示例或文档@
我一步一步地按照说明操作,但是当我运行我的代码时,我得到了这个异常
{System.NullReferenceException:当前上下文/活动为空, 确保 MainApplication.cs 文件正在设置 CurrentActivity 在您的源代码中,以便 In App Billing 可以使用它。在 Plugin.InAppBilling.InAppBillingImplementation.get_Context () [0x00000] 在 C:\projects\inappbillingplugin\src\Plugin.InAppBilling.Android\InAppBillingImplementation.cs:59 在 Plugin.InAppBilling.InAppBillingImplementation.ConnectAsync (Plugin.InAppBilling.Abstractions.ItemType itemType) [0x00000] 在 C:\projects\inappbillingplugin\src\Plugin.InAppBilling.Android\InAppBillingImplementation.cs:372 在 myproject.MainPage+d__28.MoveNext () [0x00051] 在 C:\Users\xuser\source\repos\myproject\MainPage.xaml.cs:415 }
我的示例代码
此代码在(共享/可移植类库/.NET 标准)中
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
void OnPurchased(object sender, EventArgs e)
{
InAppBilling();
}
async void InAppBilling()
{
try
{
var productId = "xxxxx.xxxx_appbilling";
var connected = await CrossInAppBilling.Current.ConnectAsync();
if (!connected)
{
//Couldn't connect to billing, could be offline, alert user
return;
}
//try to purchase item
var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload");
if (purchase == null)
{
//Not purchased, alert the user
}
else
{
//Purchased, save this information
var id = purchase.Id;
var token = purchase.PurchaseToken;
var state = purchase.State;
}
}
catch (Exception ex)
{
//Something bad has occurred, alert user
}
finally
{
//Disconnect, it is okay if we never connected
await CrossInAppBilling.Current.DisconnectAsync();
}
}
}
Droid 项目中的这段代码
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
}
}
【问题讨论】:
标签: c# android xamarin xamarin.forms xamarin.android