首先,我使用我自己的类 CurrentAppProxy,它在调试模式下使用 CurrentAppSimulator,在发布模式下使用 CurrentApp。然后我使用此代码购买 InApp 购买,它工作得很好:
try
{
await CurrentAppProxy.RequestProductPurchaseAsync(PremiumName, false);
// after closing the inApp purchase dialog, test, if it was bought
if (licenseInformation.ProductLicenses[PremiumName].IsActive)
{
// set the local flag
IsPremium = true;
dialog = new MessageDialog(resourceLoader.GetString("ThanksForPremium"));
await dialog.ShowAsync();
}
// else do not show anything
}
catch (Exception)
{
// failed buying the premium
dialog = new MessageDialog(resourceLoader.GetString("ErrorBuyingPremium"));
dialog.ShowAsync();
}
编辑:在访问这些属性之前,我使用以下命令在调试模式下初始化 CurrentAppSimulator:
StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFile proxyFile = await proxyDataFolder.GetFileAsync("test-purchase.xml");
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
在 test-purchase.xml 的底部,我得到了:
<LicenseInformation>
<App>
<IsActive>true</IsActive>
<IsTrial>false</IsTrial>
</App>
<Product ProductId="premium">
<IsActive>false</IsActive>
</Product>
</LicenseInformation>