啟用應用程式和附加元件的應用程式內購買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. 例如,若使用者目前擁有的是試用版 App,您可以使用這個程序來讓該使用者購買完整版授權。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 命名空間提供了幾個方法可要求購買 App 或附加元件: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) 中引進,只適用於目標為 Visual Studio 中 Windows 10 Anniversary Edition (10.0;組建 14393) 或更新版本的專案。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. 如果您的 app 目標為較早版本的 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 Anniversary Edition (10.0;組建 14393) 或更新版本的通用 Windows 平台 (UWP) App 的 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.
  • 您已在合作夥伴中心中 建立提交應用 程式,並在存放區中發佈此應用程式。You have created an app submission in Partner Center 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 Partner Center.

這個範例中的程式碼假設:The code in this example assumes:

  • 程式碼會在 Page 的內容中執行,其中包含名為 workingProgressRingProgressRing 和名為 textBlockTextBlockThe 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.
  • App 是單一使用者 app,僅會在啟動 app 的使用者內容中執行。The app is a single-user app that runs only in the context of the user that launched the app. 如需詳細資訊,請參閱 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 方法來購買已知其 Store 識別碼的 App 或附加元件。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

觀看下列影片,了解如何在應用程式中實作 App 內購買項目。Watch the following video for an overview of how to implement in-app purchases in your app.