c# - Windows Phone 8.1 和 CurrentAppSimulator

标签 c# windows-phone windows-store-apps winrt-xaml windows-phone-8.1

我正在尝试将应用程序内购买添加到我的通用应用程序中,但在 Windows Phone 版本中测试它时遇到了问题。该指南说,为了使用 CurrentAppSimulator我必须“在 "WindowsStoreProxy.xml" 中自定义名为 %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData 的文件”。

不过我无法在手机上执行此操作,因为我无权访问手机的文件系统。如何启用 CurrentAppSimulator

最佳答案

  • 适用于 Windows Phone 8.1、Dev Studio 2013

您可以使用位于此处的“ISETool”访问隔离存储文件:“Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\IsolatedStorageExplorerTool”。 How to use the Isolated Storage Explorer tool for Windows Phone 8 . 将它们复制到计算机上的文件夹后,您将拥有一个子文件夹“IsolatedStore”。在“\Microsoft\Windows Store\ApiData”中,您会找到“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>

将此文件复制到您的 Assets 文件夹并将其包含在您的项目中。更改文件,您需要将 ProductId 从“1”更改为“您的产品 ID”。如果您不需要应用程序产品中的消耗品,您可以删除产品 id="2"的产品。将第二个license信息修改为:

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

让你的应用程序的构造函数看起来像这样:

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

添加一个事件处理器:

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

将类似这样的内容添加到您的 App 类中:

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
            }
        }            
    }

还有引用资料..

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

关于c# - Windows Phone 8.1 和 CurrentAppSimulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25705915/

相关文章:

c# - 我必须做什么才能使我的方法可等待?

c# - 无法将动态表实体转换为自定义实体 Azure 表存储

visual-studio - Apple Mac 上带有 PC 键盘的 Windows Phone 模拟器

android - 如何查找和播放通过 Lynda.com Android 或 Windows 应用程序下载的视频

c# - Windows Phone 后台传输请求问题

c# - 文本框只能大写

c# - 检查用户控件是否在窗口范围内

c# - 使用 Async 避免重复代码

c# - 更改 DataGridView 中列中的字体大小

c# - 如何使用 WPF XAML 构造堆叠表单?