實作應用程式的試用版Implement a trial version of your app

如果您 將應用程式設定為合作夥伴中心中的免費試用版 ,讓客戶可以在試用期間免費使用您的應用程式,您可以藉由在試用期間內排除或限制某些功能,來誘讓客戶升級為應用程式的完整版本。If you configure your app as a free trial in Partner Center so that customers can use your app for free during a trial period, you can entice your customers to upgrade to the full version of your app by excluding or limiting some features during the trial period. 開始撰寫程式碼之前,請先決定哪些功能應受到限制,然後確定只有在客戶購買完整授權後,App 才會允許這些功能運作。Determine which features should be limited before you begin coding, then make sure that your app only allows them to work when a full license has been purchased. 您也可以啟用橫幅或浮水印之類的功能,這些功能僅在客戶購買您的應用程式之前的試用期間顯示。You can also enable features, such as banners or watermarks, that are shown only during the trial, before a customer buys your app.

本文顯示如何使用 Windows.Services.Store 命名空間中 StoreContext 類別的成員,以判斷使用者是否有您應用程式的試用授權,而且如果授權狀態在應用程式執行期間變更,則會收到通知。This article shows how to use members of the StoreContext class in the Windows.Services.Store namespace to determine if the user has a trial license for your app and be notified if the state of the license changes while your app is running.

注意

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.

實作試用版的指導方針Guidelines for implementing a trial version

App 目前的授權狀態會儲存為 StoreAppLicense 類別的屬性。The current license state of your app is stored as properties of the StoreAppLicense class. 一般而言,您會將依存於授權狀態的函式放在條件性區塊中,如下個步驟所述。Typically, you put the functions that depend on the license state in a conditional block, as we describe in the next step. 考量這些功能時,請確定您實作功能的方式,可在所有授權狀態下運作。When considering these features, make sure you can implement them in a way that will work in all license states.

此外,決定您在應用程式執行時要如何處理應用程式授權的變更。Also, decide how you want to handle changes to the app's license while the app is running. 您的試用版應用程式可具備完整功能,但應用程式內會有付費版本所沒有的廣告橫幅。Your trial app can be full-featured, but have in-app ad banners where the paid-for version doesn't. 或者,試用版應用程式可以停用特定功能,或是定期顯示訊息,詢問使用者是否要購買。Or, your trial app can disable certain features, or display regular messages asking the user to buy it.

考慮您正在製作的應用程式類型,以及適合採用哪種試用或到期策略。Think about the type of app you're making and what a good trial or expiration strategy is for it. 對於遊戲試用版,採用的策略最好是限制使用者可玩的遊戲內容。For a trial version of a game, a good strategy is to limit the amount of game content that a user can play. 對於試用版公用程式,您可考慮設定到期日,或是限制潛在買家會使用的功能。For a trial version of a utility, you might consider setting an expiration date, or limiting the features that a potential buyer can use.

對於大部分非遊戲類型的應用程式,設定到期日是一種很好的做法,因為使用者可以對完整的應用程式有比較深入的了解。For most non-gaming apps, setting an expiration date works well, because users can develop a good understanding of the complete app. 這裡提供幾個常見的到期日案例以及您如何處理的選項。Here are a few common expiration scenarios and your options for handling them.

  • 試用授權在應用程式執行時到期Trial license expires while the app is running

    如果您的應用程式正在執行時試用到期,應用程式可以:If the trial expires while your app is running, your app can:

    • 不執行任何動作。Do nothing.
    • 對客戶顯示訊息。Display a message to your customer.
    • 很接近了。Close.
    • 提示客戶購買應用程式。Prompt your customer to buy the app.

    最佳做法是顯示一個提示購買應用程式的訊息;如果客戶購買應用程式,就啟用所有功能讓他們繼續使用。The best practice is to display a message with a prompt for buying the app, and if the customer buys it, continue with all features enabled. 如果客戶決定不要購買,就關閉應用程式,或定期提示他們購買應用程式。If the user decides not to buy the app, close it or remind them to buy the app at regular intervals.

  • 試用授權在應用程式啟動之前到期Trial license expires before the app is launched

    如果試用期在使用者啟動應用程式之前就已到期,應用程式將無法啟動。If the trial expires before the user launches the app, your app won't launch. 使用者將會看到一個對話方塊,提供他們從 Microsoft Store 購買應用程式的選項。Instead, users see a dialog box that gives them the option to purchase your app from the Store.

  • 客戶在應用程式執行時購買應用程式Customer buys the app while it is running

    如果客戶在您的應用程式執行時購買它,這裡是您應用程式可以採取的動作。If the customer buys your app while it is running, here are some actions your app can take.

    • 什麼也不做,讓客戶在試用模式下繼續使用,直到重新啟動應用程式。Do nothing and let them continue in trial mode until they restart the app.
    • 感謝他們購買,或是顯示一則訊息。Thank them for buying or display a message.
    • 不顯示訊息直接啟用完整授權的所有功能 (或停用試用版通知)。Silently enable the features that are available with a full-license (or disable the trial-only notices).

務必對客戶說明您的 App 在免費試用期間或到期之後的行為,客戶才不會對 App 的行為感到意外。Be sure to explain how your app will behave during and after the free trial period so your customers won't be surprised by your app's behavior. 如需有關描述 App 的詳細資訊,請參閱建立 App 描述For more info about describing your app, see Create app descriptions.

先決條件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 in Partner Center that is configured as a free trial with no time limit 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.

這個範例中的程式碼假設: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

當您的 App 進行初始化時,請取得 App 的 StoreAppLicense 物件,並處理 OfflineLicensesChanged 事件,以在授權於 App 執行期間變更時收到通知。When your app is initializing, get the StoreAppLicense object for your app and handle the OfflineLicensesChanged event to receive notifications when the license changes while the app is running. 例如,如果試用期到期,或是客戶透過 Microsoft Store 購買 App,則 App 的授權會有所變更。For example, the app's license could change if the trial period expires or the customer buys the app through a Store. 當授權變更時,取得新的授權,並據以啟用或停用您的 App 功能。When the license changes, get the new license and enable or disable a feature of your app accordingly.

此時,如果使用者購買 App,最好可以對使用者提供授權狀態有所變更的回應。At this point, if a user bought the app, it is a good practice to provide feedback to the user that the licensing status has changed. 根據程式碼的撰寫方式,您可能必須要求使用者重新啟動應用程式。You might need to ask the user to restart the app if that's how you've coded it. 但請盡可能讓轉換流暢、輕鬆。But make this transition as seamless and painless as possible.

private StoreContext context = null;
private StoreAppLicense appLicense = null;

// Call this while your app is initializing.
private async void InitializeLicense()
{
    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;
    appLicense = await context.GetAppLicenseAsync();
    workingProgressRing.IsActive = false;

    // Register for the licenced changed event.
    context.OfflineLicensesChanged += context_OfflineLicensesChanged;
}

private async void context_OfflineLicensesChanged(StoreContext sender, object args)
{
    // Reload the license.
    workingProgressRing.IsActive = true;
    appLicense = await context.GetAppLicenseAsync();
    workingProgressRing.IsActive = false;

    if (appLicense.IsActive)
    {
        if (appLicense.IsTrial)
        {
            textBlock.Text = $"This is the trial version. Expiration date: {appLicense.ExpirationDate}";

            // Show the features that are available during trial only.
        }
        else
        {
            // Show the features that are available only with a full license.
        }
    }
}

如需完整的範例應用程式,請參閱市集範例For a complete sample application, see the Store sample.