应用内购买 - 应用程序让我在导航后再次购买

时间:2014-05-22 16:21:28

标签: c# windows-store-apps in-app-purchase

我很想为我的应用添加应用内购买选项。 我可以正确购买产品,但是当我购买并导航到其他页面并返回购买页面时,我仍然可以再次购买相同的产品。当我购买另一种产品时,我购买的旧产品会被购买。

这是我的代码:

    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();
        }

    }

谢谢,对不起我的英文

1 个答案:

答案 0 :(得分:1)

您每次导航时都会重新加载Store模拟器XML文件,这将重置您之前完成的任何商店活动。更改代码,以便仅在应用程序启动时加载XML,然后您应该看到Store状态在导航中保持自身状态。另请注意,当您重新启动应用程序时,您将重新加载XML,从而重置状态。