我想在我的Windows通用应用程序中集成应用内购买。在编码之前,我需要执行以下步骤:
在Windows Dev Center上创建应用程序
在IAPs部分添加产品详细信息,并提交到商店,如图像所示
- 之后,我在我的应用程序中使用以下代码获取应用内购买产品列表和购买产品的按钮。我还在我的代码中使用了
CurrentApp
而不是CurrentAppSimulator
,但它会抛出异常。
private async void RenderStoreItems()
{
picItems.Clear();
try
{
//StoreManager mySM = new StoreManager();
ListingInformation li = await CurrentAppSimulator.LoadListingInformationAsync();
System.Diagnostics.Debug.WriteLine(li);
foreach (string key in li.ProductListings.Keys)
{
ProductListing pListing = li.ProductListings[key];
System.Diagnostics.Debug.WriteLine(key);
string status = CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive ? "Purchased" : pListing.FormattedPrice;
string imageLink = string.Empty;
picItems.Add(
new ProductItem
{
imgLink = key.Equals("BaazarMagzine101") ? "block-ads.png" : "block-ads.png",
Name = pListing.Name,
Status = status,
key = key,
BuyNowButtonVisible = CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive ? false : true
}
);
}
pics.ItemsSource = picItems;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
private async void ButtonBuyNow_Clicked(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string key = btn.Tag.ToString();
if (!CurrentAppSimulator.LicenseInformation.ProductLicenses[key].IsActive)
{
ListingInformation li = await CurrentAppSimulator.LoadListingInformationAsync();
string pID = li.ProductListings[key].ProductId;
string receipt = await CurrentAppSimulator.RequestProductPurchaseAsync(pID, true);
System.Diagnostics.Debug.WriteLine(receipt);
// RenderStoreItems();
}
}
我将我的应用与商店关联,并且应用程序包与MS Dev Center应用程序中的相同,您可以在图像中看到。
当我运行我的应用并点击“购买”按钮时,就会出现对话框,就像您在图像中看到的那样,之后我没有从商店获取收据数据。
如果我做错了,请给我提供正确的指导,以实现应用内购买并在我的笔记本设备上测试该应用内购买。