Search code examples
c#windows-phonewindows-store-appswinrt-xamlwindows-phone-8.1

Windows Phone 8.1 and CurrentAppSimulator


I'm trying to add in app purchases to my universal app and am having trouble testing it in the Windows Phone version. The guide says that in order to use the CurrentAppSimulator I must "customize the file named "WindowsStoreProxy.xml" in %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData".

I can't do this on the phone though, as I don't have access to the phone's file system. How do I enable the CurrentAppSimulator?


Solution

    • Valid for Windows Phone 8.1, Dev Studio 2013

    You can access the isolated storage files using the "ISETool" located here: "Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\IsolatedStorageExplorerTool". How to use the Isolated Storage Explorer tool for Windows Phone 8. After copying them to a folder on your computer you will have a subfolder "IsolatedStore". Inside "\Microsoft\Windows Store\ApiData" you will find "WindowsStoreProxy.xml":

    <?xml version="1.0" encoding="utf-16" ?>
    <CurrentApp>
        <ListingInformation>
            <App>
            <AppId>00000000-0000-0000-0000-000000000000</AppId>
            <LinkUri>http://apps.microsoft.com/webpdp/app/00000000-0000-0000-0000-000000000000</LinkUri>
            <CurrentMarket>en-US</CurrentMarket>
            <AgeRating>3</AgeRating>
            <MarketData xml:lang="en-us">
                <Name>AppName</Name>
                <Description>AppDescription</Description>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </App>
        <Product ProductId="1" LicenseDuration="0" ProductType="Durable">
            <MarketData xml:lang="en-us">
                <Name>Product1Name</Name>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </Product>
        <Product ProductId="2" LicenseDuration="0" ProductType="Consumable">
            <MarketData xml:lang="en-us">
                <Name>Product2Name</Name>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </Product>
    </ListingInformation>
    <LicenseInformation>
        <App>
            <IsActive>true</IsActive>
            <IsTrial>true</IsTrial>
        </App>
        <Product ProductId="1">
            <IsActive>true</IsActive>
        </Product>
    </LicenseInformation>
    <ConsumableInformation>
        <Product ProductId="2" TransactionId="00000000-0000-0000-0000-000000000000" Status="Active" />
    </ConsumableInformation>
    

    Copy this file to your assets folder and include it in your project. Change the file, you need to change the ProductId from "1" to "Your Product ID". you can remove the product with product id="2" if you don't need a consumable in app product. Change the second license information to:

    <LicenseInformation>
        <App>
            <IsActive>true</IsActive>
            <IsTrial>false</IsTrial>
        </App>
        <Product ProductId="Your Product ID">
            <IsActive>false</IsActive>
        </Product>
    </LicenseInformation>
    

    Make the constructor of your app look something like this:

    private static LicenseInformation licenseInformation=null;
    public App()
        {
            this.InitializeComponent();
            this.Suspending += this.OnSuspending;
    
    #if DEBUG
            licenseInformation = CurrentAppSimulator.LicenseInformation;
    #else
            licenseInformation = CurrentApp.LicenseInformation; 
    #endif
            licenseInformation.LicenseChanged += licenseInformation_LicenseChanged;            
        }
    

    Add an event handler:

    private static void licenseInformation_LicenseChanged()
    {
        if (licenseInformation.ProductLicenses["Your Product ID"].IsActive)
        {
            // add code for purchased (e.g. property of a data class derived from INotifyPropertyChanged)
        }
        else
        {
            // add code for not yet purchased (e.g. property of a data class derived from INotifyPropertyChanged)
        }
    }
    

    Add something like this to your App class:

    public static async void RequestFeatureXYZ()
        {
            if (!licenseInformation.ProductLicenses["Your Product ID"].IsActive)
            {
                try
                {
    #if DEBUG
                    StorageFolder installFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
                    StorageFile appSimulatorStorageFile = await installFolder.GetFileAsync("WindowsStoreProxy.xml");                                      
    
                    await CurrentAppSimulator.ReloadSimulatorAsync(appSimulatorStorageFile);
                    PurchaseResults result = await CurrentAppSimulator.RequestProductPurchaseAsync("Your Product ID");
    #else
                    PurchaseResults result = await CurrentApp.RequestProductPurchaseAsync("Your Product ID");
    #endif
                    // licenseInformation_LicenseChanged(); // (un)comment if you find out that the event does (not) always fire                }
                catch (Exception ex)
                {
                    // handle error or do nothing
                }
            }            
        }
    

    And the references..

    using Windows.Storage;
    using Windows.Storage.Streams;
    using Windows.ApplicationModel.Store;
    using System.ComponentModel;