应用内购买-应用程序让我再次购买后导航
本文关键字:导航 应用程序 应用 | 更新日期: 2023-09-27 18:04:43
我正试图为我的应用程序添加应用内购买选项。我可以正确购买产品,但当我购买并导航到其他页面并返回购买页面时,我仍然可以再次购买相同的产品。当我购买另一种产品时,我购买的旧产品变为未购买。
下面是我的代码:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
LicenseChangedEventHandler licenseChangeHandler = null;
public SatinAlma()
{
this.InitializeComponent();
}
private 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);
try
{
ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
var PurchasePzl9 = listing.ProductListings["puzzle9"];
var PurchasePzl16 = listing.ProductListings["puzzle16"];
var PurchasePzl25 = listing.ProductListings["puzzle25"];
var PurchaseYardimseverlik = listing.ProductListings["yardimseverlik"];
var PurchaseTumpaket = listing.ProductListings["tumpaket"];
}
catch (Exception)
{
OAL_toast.showToast("LoadListingInformationAsync API call failed'n");
}
}
///////**i insert and delete this part nothing changed
//protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
//{
// if (licenseChangeHandler != null)
// {
// CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
// }
// base.OnNavigatingFrom(e);
//}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
await LoadInAppPurchaseProxyFileAsync();
}
private void InAppPurchaseRefreshScenario()
{
}
private async void purchaseProduct()
{
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses[stringPurchaseProduct].IsActive)
{
try
{
await CurrentAppSimulator.RequestProductPurchaseAsync(stringPurchaseProduct);
if (licenseInformation.ProductLicenses[stringPurchaseProduct].IsActive)
{
OAL_toast.showToast(stringPurchaseProduct + " purchased.");
this.Frame.Navigate(typeof(MainPage));
}
else
{
//OptimeAnimationLib.MsgBox(stringPurchaseProduct + " was not purchased.");
OAL_toast.showToast(stringPurchaseProduct + " was not purchased.");
}
}
catch (Exception)
{
OAL_toast.showToast("Unable to buy " + stringPurchaseProduct);
}
}
else
{
OAL_toast.showToast("you already own " + stringPurchaseProduct);
}
}
private void IMG_puzzle9_PointerReleased(object sender, PointerRoutedEventArgs e)
{
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
var productLicense = licenseInformation.ProductLicenses["puzzle9"];
if (productLicense.IsActive)
{
OAL_toast.showToast("you already own Puzzle 9.");
}
else
{
stringPurchaseProduct = "puzzle9";
purchaseProduct();
}
}
thanks, sorry for my english
看起来您每次导航时都在重新加载Store模拟器XML文件,这将重置您之前所做的任何Store活动。修改代码,只在应用程序启动时加载XML,然后你应该看到Store状态在各个导航中保持自己。还要注意,当你重新启动应用程序时,你将重新加载XML,从而重置状态。