共用方式為


啟用應用程式和附加元件的應用程式內購買

本文示範如何使用 Windows.Services.Store 命名空間中的成員,要求為使用者購買目前的應用程式或其中一個附加元件。 例如,如果使用者目前有應用程式的試用版,您可以使用此程序為使用者購買完整授權。 或者,您可以使用此程序來購買附加元件,例如使用者的新遊戲關卡。

若要要求購買應用程式或附加元件,Windows.Services.Store 命名空間提供數種不同的方法:

每個方法都會向使用者呈現標準購買 UI,然後在交易完成之後以非同步方式完成。 方法會傳回物件,指出交易是否成功。

注意

Windows.Services.Store 命名空間是在 Windows 10 版本 1607 中引進的,而且在 Visual Studio 中只能用於以 Windows 10 年度版本 (10.0;組建 14393) 或更新版本為目標的專案。 如果您的應用程式以舊版 Windows 10 為目標,您必須使用 Windows.ApplicationModel.Store 命名空間,而不是 Windows.Services.Store 命名空間。 如需詳細資訊,請參閱這篇文章

必要條件

此範例具有以下必要條件:

  • Windows 10 年度版本 (10.0;組建 14393) 或更新版本為目標之通用 Windows 平台 (UWP) 應用程式的 Visual Studio 專案。
  • 您已在合作夥伴中心建立應用程式提交,且此應用程式會在市集中發佈。 您可以選擇將應用程式設定為在您進行測試時無法在市集中探索。 如需詳細資訊,請參閱我們測試指導方針
  • 如果您想要為應用程式的附加元件啟用應用程式內購買,您還必須在合作夥伴中心建立附加元件

此範例中的程式碼假設:

  • 程序碼會在 Page 的內容中執行,其中包含名為 workingProgressRingProgressRing 和名為 textBlockTextBlock。 這些物件是用來指出非同步作業正在發生,以及分別顯示輸出訊息。
  • 程式碼檔案具有 Windows.Services.Store 命名空間的 using 陳述式。
  • 應用程式是單一使用者應用程式僅會在啟動應用程式的使用者內容中執行。 如需詳細資訊,請參閱應用程式內購買和試用版

注意

如果您有使用傳統型橋接器的傳統型應用程式,可能需要新增未顯示在本範例中的其他程式碼來設定 StoreContext 物件。 如需詳細資訊,請參閱在使用傳統型橋接器的傳統型應用程式中使用 StoreContext 類別

程式碼範例

此範例示範如何使用 StoreContext 類別的 RequestPurchaseAsync 方法來購買具有已知 Store ID 的應用程式或附加元件。 如需完整的範例應用程式,請參閱市集範例

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