支持应用内购买应用和加载项Enable in-app purchases of apps and add-ons

本文介绍了如何使用 Windows.Services.Store 命名空间中的成员,请求为用户购买当前应用或其加载项之一。This article demonstrates how to use members in the Windows.Services.Store namespace to request the purchase the current app or one of its add-ons for the user. 例如,如果用户当前有应用的试用版,可以使用此过程为用户购买完整版。For example, if the user currently has a trial version of the app, you can use this process to purchase a full license for the user. 或者,你可以使用此过程购买加载项,如用户的新游戏关卡。Alternatively, you can use this process to purchase an add-on, such as a new game level for the user.

若要请求购买应用或加载项,Windows.Services.Store 命名空间提供了几个不同方法:To request the purchase of an app or add-on, the Windows.Services.Store namespace provides several different methods:

每个方法都为用户提供标准购买 UI,随后在交易完成后异步完成这些方法。Each method presents a standard purchase UI to the user and then completes asynchronously after the transaction is complete. 该方法返回一个指示该交易是否已成功的对象。The method returns an object that indicates whether the transaction was successful.

备注

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 10 的较早版本,则必须使用 Windows.ApplicationModel.Store 命名空间,而不是 Windows.Services.Store 命名空间。If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. 有关详细信息,请参阅此文For more information, see this article.

先决条件Prerequisites

本示例有以下先决条件:This example has the following prerequisites:

  • 适用于面向 Windows 10 周年纪念版(10.0;版本 14393)或更高版本的通用 Windows 平台 (UWP) 应用的 Visual Studio 项目。A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
  • 你已在 Windows 开发人员中心仪表板中创建了一个应用提交,并且该应用已发布到应用商店中。You have created an app submission in the Windows Dev Center dashboard and this app is published in the Store. 在测试应用期间,你可以选择将应用配置为在应用商店中隐藏。You can optionally configure the app so it is not discoverable in the Store while you test it. 有关详细信息,请参阅我们的测试指南For more information, see our testing guidance.
  • 如果你想要对应用的某个加载项启用应用内购买,还必须在开发人员中心仪表板中创建加载项If you want to enable in-app purchases for an add-on for the app, you must also create the add-on in the Dev Center dashboard.

此示例中的代码假设:The code in this example assumes:

  • 代码在含有 ProgressRing(名为 workingProgressRing)和 TextBlock(名为 textBlock)的 Page 上下文中运行。The code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. 这些对象分别用于指示是否正在进行异步操作和显示输出消息。These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • 代码文件有一个适用于 Windows.Services.Store 命名空间的 using 语句。The code file has a using statement for the Windows.Services.Store namespace.
  • 该应用是单用户应用,仅在启动该应用的用户上下文中运行。The app is a single-user app that runs only in the context of the user that launched the app. 有关详细信息,请参阅应用内购买和试用For more information, see In-app purchases and trials.

备注

如果你有使用桌面桥的桌面应用程序,可能需要添加不在此示例中显示的额外代码来配置 StoreContext 对象。If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in this example to configure the StoreContext object. 有关详细信息,请参阅在使用桌面桥的桌面应用程序中使用 StoreContext 类For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

代码示例Code example

此示例演示如何使用 StoreContext 类的 RequestPurchaseAsync 方法,购置具有已知应用商店 ID 的应用或加载项。This example demonstrates how to use the RequestPurchaseAsync method of the StoreContext class to purchase an app or add-on with a known Store ID. 有关完整的示例应用程序,请参阅应用商店示例For a complete sample application, see the Store sample.

private StoreContext context = null;

public async void PurchaseAddOn(string storeId)
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    workingProgressRing.IsActive = true;
    StorePurchaseResult result = await context.RequestPurchaseAsync(storeId);
    workingProgressRing.IsActive = false;

    // Capture the error message for the operation, if any.
    string extendedError = string.Empty;
    if (result.ExtendedError != null)
    {
        extendedError = result.ExtendedError.Message;
    }

    switch (result.Status)
    {
        case StorePurchaseStatus.AlreadyPurchased:
            textBlock.Text = "The user has already purchased the product.";
            break;

        case StorePurchaseStatus.Succeeded:
            textBlock.Text = "The purchase was successful.";
            break;

        case StorePurchaseStatus.NotPurchased:
            textBlock.Text = "The purchase did not complete. " +
                "The user may have cancelled the purchase. ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.NetworkError:
            textBlock.Text = "The purchase was unsuccessful due to a network error. " +
                "ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.ServerError:
            textBlock.Text = "The purchase was unsuccessful due to a server error. " +
                "ExtendedError: " + extendedError;
            break;

        default:
            textBlock.Text = "The purchase was unsuccessful due to an unknown error. " +
                "ExtendedError: " + extendedError;
            break;
    }
}

视频Video

观看以下视频,大致了解如何在你的应用中实现应用内购买。Watch the following video for an overview of how to implement in-app purchases in your app.