启用应用内产品购买Enable in-app product purchases

无论你的应用是否免费,你都可以直接从应用中销售内容、其他应用或新的应用功能(例如解锁游戏的下一关)。Whether your app is free or not, you can sell content, other apps, or new app functionality (such as unlocking the next level of a game) from right within the app. 我们在此处显示了如何在应用中启用这些产品。Here we show you how to enable these products in your app.


本文介绍如何使用 Windows.ApplicationModel.Store 命名空间的成员来支持应用内产品购买。This article demonstrates how to use members of the Windows.ApplicationModel.Store namespace to enable in-app product purchases. 此命名空间不再更新新功能,我们建议你使用 Windows.Services.Store 命名空间。This namespace is no longer being updated with new features, and we recommend that you use the Windows.Services.Store namespace instead. Windows.Services.Store 命名空间支持最新的加载项类型(如应用商店管理的易耗型加载项和订阅),并且设计为与 Windows 开发人员中心和应用商店将来支持的产品和功能类型兼容。The Windows.Services.Store namespace supports the latest add-on types, such as Store-managed consumable add-ons and subscriptions, and is designed to be compatible with future types of products and features supported by Windows Dev Center and the Store. Windows.Services.Store 命名空间在 Windows 10 版本 1607 中引入,它仅可用于面向 Windows 10 周年纪念版(10.0;版本 14393)或 Visual Studio 更高版本的项目中。The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. 有关启用应用程序中产品购买使用Windows.Services.Store命名空间的详细信息,请参阅这篇文章For more information about enabling in-app product purchases using the Windows.Services.Store namespace, see this article.


试用版应用不能提供应用内产品。In-app products cannot be offered from a trial version of an app. 仅当使用试用版应用的客户购买了完整版应用后,他们才可以购买应用内产品。Customers using a trial version of your app can only buy an in-app product if they purchase a full version of your app.


  • 可添加供客户购买的功能的 Windows 应用。A Windows app in which to add features for customers to buy.
  • 首次编码和测试新的应用内产品时,必须使用 CurrentAppSimulator 对象而不是 CurrentApp 对象。When you code and test new in-app products for the first time, you must use the CurrentAppSimulator object instead of the CurrentApp object. 这样,你可以使用对许可证服务器的模拟调用验证许可证逻辑,而不是调用实时服务器。This way you can verify your license logic using simulated calls to the license server instead of calling the live server. 若要实现此目的,需要在 %userprofile%\AppData\local\packages\<程序包名称>\LocalState\Microsoft\Windows Store\ApiData 中自定义名为“WindowsStoreProxy.xml”的文件。To do this, you need to customize the file named WindowsStoreProxy.xml in %userprofile%\AppData\local\packages\<package name>\LocalState\Microsoft\Windows Store\ApiData. Microsoft Visual Studio 仿真器会在你首次运行应用时创建此文件,你也可以在运行时加载一个自定义文件。The Microsoft Visual Studio simulator creates this file when you run your app for the first time—or you can also load a custom one at runtime. 有关详细信息,请参阅将 WindowsStoreProxy.xml 文件与 CurrentAppSimulator 一起使用For more info, see Using the WindowsStoreProxy.xml file with CurrentAppSimulator.
  • 本主题还参考了应用商店示例中提供的代码示例。This topic also references code examples provided in the Store sample. 若要获得为通用 Windows 平台 (UWP) 应用提供的不同货币化选项的实际体验,此示例是一个不错的选择。This sample is a great way to get hands-on experience with the different monetization options provided for Universal Windows Platform (UWP) apps.

步骤 1:为你的应用初始化许可证信息Step 1: Initialize the license info for your app

当应用在执行初始化时,请通过初始化 CurrentAppCurrentAppSimulator 获取应用的 LicenseInformation 对象,从而启用应用内产品的购买。When your app is initializing, get the LicenseInformation object for your app by initializing the CurrentApp or CurrentAppSimulator to enable purchases of an in-app product.

void InitializeApp()
    // Some app initialization code...

    // Initialize the license info for use in the app that is uploaded to the Store.
    // Uncomment the following line in the release version of your app.
    //   licenseInformation = CurrentApp.LicenseInformation;

    // Initialize the license info for testing.
    // Comment the following line in the release version of your app.
    licenseInformation = CurrentAppSimulator.LicenseInformation;

    // Other app initialization code...

步骤 2:向应用添加应用内付费内容Step 2: Add the in-app offers to your app

针对你希望通过应用内产品提供的每项功能,创建一个付费内容并将它添加到你的应用中。For each feature that you want to make available through an in-app product, create an offer and add it to your app.


向应用商店提交你的应用之前,必须将你希望呈现给客户的所有应用内产品都添加到应用中。You must add all the in-app products that you want to present to your customers to your app before you submit it to the Store. 如果你稍后需要添加新的应用内产品,则必须更新应用并重新提交新版本。If you want to add new in-app products later, you must update your app and re-submit a new version.

  1. 创建应用内付费内容标记Create an in-app offer token

    通过标记来标识应用中的每个应用内产品。You identify each in-app product in your app by a token. 此标记是一个字符串,在你的应用和应用商店中定义和使用它来标识特定的应用内产品。This token is a string that you define and use in your app and in the Store to identify a specific in-app product. 为它提供一个独特(对于你的应用)且有意义的名称,以便你可以在编码时快速标识它所表示的正确功能。Give it a unique (to your app) and meaningful name so that you can quickly identify the correct feature it represents while you are coding. 下面是一些名称示例:Here are some examples of names:

    • “SpaceMissionLevel4”"SpaceMissionLevel4"
    • “ContosoCloudSave”"ContosoCloudSave"
    • “RainbowThemePack”"RainbowThemePack"


    你在代码中使用的应用内优惠标记必须与你在开发人员中心仪表板中为你的应用定义相应加载项时指定的产品 ID 值相匹配。The in-app offer token that you use in your code must match the product ID value you specify when you define the corresponding add-on for your app in the Dev Center dashboard.

  2. 在条件块中编码功能Code the feature in a conditional block

    必须将与应用内产品关联的每个功能的代码都放在条件块中,从而进行测试以查看客户是否有使用该功能的许可证。You must put the code for each feature that is associated with an in-app product in a conditional block that tests to see if the customer has a license to use that feature.

    下面的示例介绍了如何在特定于许可证的条件块中编码名为 featureName 的产品功能。Here's an example that shows how you can code a product feature named featureName in a license-specific conditional block. 字符串 featureName 是在应用中唯一标识此产品的标记,它也用于在应用商店中标识此产品。The string, featureName,is the token that uniquely identifies this product within the app and is also used to identify it in the Store.

    if (licenseInformation.ProductLicenses["featureName"].IsActive)
        // the customer can access this feature
        // the customer can' t access this feature
  3. 为此功能添加购买 UIAdd the purchase UI for this feature

    你的应用还必需为客户提供一种方式来购买应用内产品提供的产品或功能。Your app must also provide a way for your customers to purchase the product or feature offered by the in-app product. 客户无法以购买完整应用的方式通过应用商店来购买上述内容。They can't purchase them through the Store in the same way they purchased the full app.

    下面介绍如何测试,以查看客户是否已拥有应用内产品;如果尚未拥有,则显示购买对话框,让用户可以购买该内容。Here's how to test to see if your customer already owns an in-app product and, if they don't, displays the purchase dialog so they can buy it. 将注释“显示购买对话框”替换为你的购买对话框自定义代码(如一个包含友好的“购买此应用!”按钮的页面)Replace the comment "show the purchase dialog" with your custom code for the purchase dialog (such as a page with a friendly "Buy this app!" button).

    async void BuyFeature()
        if (!licenseInformation.ProductLicenses["featureName"].IsActive)
                // The customer doesn't own this feature, so
                // show the purchase dialog.
                await CurrentAppSimulator.RequestProductPurchaseAsync("featureName", false);
                //Check the license state to determine if the in-app purchase was successful.
            catch (Exception)
                // The in-app purchase was not completed because
                // an error occurred.
            // The customer already owns this feature.

步骤 3:更改测试代码以达到最后要求Step 3: Change the test code to the final calls

这是一个简单的步骤:在应用的代码中将对 CurrentAppSimulator 的每个引用更改为 CurrentAppThis is an easy step: change every reference to CurrentAppSimulator to CurrentApp in your app's code. 你不再需要提供 WindowsStoreProxy.xml 文件,因此请将它从你的应用路径中删除(但是你可能希望保存它,以便在下一步中配置应用内付费内容时进行引用)。You don't need to provide the WindowsStoreProxy.xml file any longer, so remove it from your app's path (although you may want to save it for reference when you configure the in-app offer in the next step).

步骤 4:在应用商店中配置应用内产品付费内容Step 4: Configure the in-app product offer in the Store

在开发人员中心仪表板中,导航到你的应用并创建加载项,使之与你的应用内产品优惠相匹配。In the Dev Center dashboard, navigate to your app and create an add-on that matches your in-app product offer. 为你的加载项定义产品 ID、类型、价格以及其他属性。Define the product ID, type, price, and other properties for your add-on. 请确保完全按照测试时在 WindowsStoreProxy.xml 中设置的配置来配置它。Make sure that you configure it identically to the configuration you set in WindowsStoreProxy.xml when testing.


你在代码中使用的应用内优惠标记必须与你在仪表板中为相应加载项指定的产品 ID 值相匹配。The in-app offer token that you use in your code must match the product ID value you specify for the corresponding add-on in the dashboard.


如果你对向客户提供可消费应用内产品选项(可购买、用完,然后根据需要再次购买的项目)感兴趣,请参阅启用可消费应用内产品购买主题。If you're interested in providing your customers with consumable in-app product options (items that can be purchased, used up, and then purchased again if desired), move on to the Enable consumable in-app product purchases topic.

如果你需要使用收据来验证用户是否进行了应用内购买,请务必查看使用收据验证产品购买If you need to use receipts to verify that user made an in-app purchase, be sure to review Use receipts to verify product purchases.