C# RequestProductPurchaseAsync抛出“;设置后无法更改线程模式”;
我使用monogame作为游戏框架,并尝试在UWP应用程序中实现应用内购买功能,当我调用C# RequestProductPurchaseAsync抛出“;设置后无法更改线程模式”;,c#,uwp,monogame,C#,Uwp,Monogame,我使用monogame作为游戏框架,并尝试在UWP应用程序中实现应用内购买功能,当我调用RequestProductPurchaseAsync时会引发异常。它说: 设置后无法更改线程模式。(HRESULT的例外情况: 0x80010106(RPC_E_已更改_模式))位于 Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(字符串 productId)在 Crocs_World___Xbox
RequestProductPurchaseAsync
时会引发异常。它说:
设置后无法更改线程模式。(HRESULT的例外情况:
0x80010106(RPC_E_已更改_模式))位于
Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(字符串
productId)在
Crocs_World___Xbox_Edition_.App.d__7.MoveNext()
这就是我在代码中所做的:
public async Task LoadInAppPurchaseProxyFileAsync()
{
StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
// setup application upsell message
try
{
ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
var product1 = listing.ProductListings["product1"];
var product2 = listing.ProductListings["product2"];
}
catch (Exception e)
{
Debug.WriteLine("LoadListingInformationAsync API call failed:" + e);
}
}
private async void InAppPurchaseRefreshScenario()
{
Debug.WriteLine("InAppPurchaseRefreshScenario");
}
public async Task BuyFeature()
{
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["product2"].IsActive)
{
Debug.WriteLine("Buying Product 2...");
try
{
await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
if (licenseInformation.ProductLicenses["product2"].IsActive)
{
Debug.WriteLine("You bought Product 2.");
}
else
{
Debug.WriteLine("Product 2 was not purchased.");
}
}
catch (Exception e)
{
Debug.WriteLine("Unable to buy Product 2." + e);
}
}
else
{
Debug.WriteLine("You already own Product 2.");
}
}
每当我调用BuyFeature
时,它都会抛出异常。除非我在LoadInAppPurchaseProxyFileAsync
中正确调用它。那么,我想这似乎是同一条线索。
如果我用两种方法中的void
替换Task
,它也不会起作用。无论我是从app.xaml.cs还是从game.cs调用它,都没有关系。
有人知道我做错了什么吗
谢谢,
Harry您必须在UI线程中调用
wait CurrentAppSimulator.RequestProductPurchaseAsync(…)
,因为如果您在发布模式下使用wait CurrentApp.RequestProductPurchaseAsync(…)
运行它,屏幕上会显示一个“购买项目”内容对话框,这仅在UI线程中可能
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
};
您必须在UI线程中调用
wait CurrentAppSimulator.RequestProductPurchaseAsync(…)
,因为如果您在发布模式下使用wait CurrentApp.RequestProductPurchaseAsync(…)
运行它,屏幕上会显示一个“购买项目”内容对话框,这仅在UI线程中可能
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
};