應用程式內購買和試用版In-app purchases and trials

Windows SDK 提供您可用來實作下列功能以從您的「通用 Windows 平台」(UWP) 應用程式提升獲利的 API。The Windows SDK provides APIs you can use to implement the following features to make more money from your Universal Windows Platform (UWP) app:

  • 應用程式內購買    無論您的應用程式是免費的,您都可以銷售內容或新的應用程式功能 (例如,在應用程式中直接解除) 下一個層級的遊戲。In-app purchases  Whether your app is free or not, you can sell content or new app functionality (such as unlocking the next level of a game) from right within the app.

  • 試用版功能    如果您將應用程式設定為合作夥伴中心中的免費試用版,您可以藉由在試用期間內排除或限制某些功能,來誘讓客戶購買完整版的應用程式。Trial functionality  If you configure your app as a free trial in Partner Center, you can entice your customers to purchase the full version of your app by excluding or limiting some features during the trial period. 您也可以啟用橫幅或浮水印之類的功能,這些功能僅在客戶購買您的應用程式之前的試用期間顯示。You can also enable features, such as banners or watermarks, that are shown only during the trial, before a customer buys your app.

本文提供 UWP 應用程式中應用程式內購買和試用版的運作方式概觀。This article provides an overview of how in-app purchases and trials work in UWP apps.

選擇要使用的命名空間Choose which namespace to use

您可以根據您應用程式的目標是哪一個 Windows 10 版本,使用兩種不同的命名空間將應用程式內購買和試用版功能新增到 UWP 應用程式。There are two different namespaces you can use to add in-app purchases and trial functionality to your UWP apps, depending on which version of Windows 10 your apps target. 雖然這些命名空間中的 API 都是為相同的目標服務,但其設計方式截然不同,且兩個 API 之間的程式碼並不相容。Although the APIs in these namespaces serve the same goals, they are designed quite differently, and code is not compatible between the two APIs.

重要

Windows.ApplicationModel.Store 命名空間不再提供新功能更新,建議您改為使用 Windows.Services.Store 命名空間,如果您的應用程式可這麼做的話。The Windows.ApplicationModel.Store namespace is no longer being updated with new features, and we recommend that you use the Windows.Services.Store namespace instead if possible for your app. 使用傳統型橋接器的 windows 桌面應用程式,或在合作夥伴中心 (使用開發沙箱的應用程式或遊戲中,不支援ApplicationModel。 例如,與 Xbox Live) 整合的所有遊戲都是如此。The Windows.ApplicationModel.Store namespace is not supported in Windows desktop applications that use the Desktop Bridge or in apps or games that use a development sandbox in Partner Center (for example, this is the case for any game that integrates with Xbox Live).

基本概念Basic concepts

「市集」中提供的每個項目通常稱為 「產品」Every item that is offered in the Store is generally called a product. 大部分的開發人員只會使用下列類型的產品︰應用程式附加元件Most developers only work with the following types of products: apps and add-ons.

附加元件是您在應用程式內容中提供給客戶使用的產品或功能︰例如,在應用程式或遊戲中使用的貨幣、適用於遊戲的新地圖或武器,能夠在沒有廣告的情況下使用您的應用程式,或者適用於能夠提供該類型內容之應用程式的數位內容 (例如,音樂或視訊)。An add-on is a product or feature that you make available to your customers in the context of your app: for example, currency to be used in an app or game, new maps or weapons for a game, the ability to use your app without ads, or digital content such as music or videos for apps that have the ability to offer that type of content. 每個應用程式與附加元件都有相關的授權,可指出使用者是否有資格使用該應用程式或附加元件。Every app and add-on has an associated license that indicates whether the user is entitled to use the app or add-on. 如果使用者有資格使用應用程式或附加元件,授權也會提供關於試用版的額外資訊。If the user is entitled to use the app or add-on as a trial, the license also provides additional info about the trial.

若要在您的應用程式中提供客戶的附加元件,您必須 在合作夥伴中心中為您的應用程式定義附加 元件,讓商店知道它的相關資訊。To offer an add-on to customers in your app, you must define the add-on for your app in Partner Center so the Store knows about it. 接著,您的應用程式便可以使用 Windows.Services.StoreWindows.ApplicationModel.Store 命名空間中的 API,以應用程式內購買形式提供要對使用者銷售的附加元件。Then, your app can use APIs in the Windows.Services.Store or Windows.ApplicationModel.Store namespace to offer the add-on for sale to the user as an in-app purchase.

UWP 應用程式可以提供下列類型的附加元件。UWP apps can offer the following types of add-ons.

附加元件類型Add-on type 說明Description
持久Durable 針對您 在合作夥伴中心中指定的存留期保存的附加元件。An add-on that persists for the lifetime that you specify in Partner Center.

根據預設,耐久性附加元件永遠不會過期,在此情況下只需購買一次。By default, durable add-ons never expire, in which case they can only be purchased once. 如果您為附加元件指定特定的持續時間,則使用者就能在該附加元件到期之後重新進行購買。If you specify a particular duration for the add-on, the user can repurchase the add-on after it expires.

開發人員管理的消費性產品Developer-managed consumable 可購買、使用,然後消費過後再次購買的附加元件。An add-on that can be purchased, used, and then purchased again after it is consumed. 您要負責記錄附加元件所代表項目的使用者餘額。You are responsible for keeping track of the user's balance of items that the add-on represents.

當使用者取用任何與附加元件相關的項目時,您需負責維持使用者在該附加元件所代表項目的餘額,以及負責在使用者取用所有項目之後,向 Microsoft Store 回報已完全交付此附加元件的購買。When the user consumes any items that are associated with the add-on, you are responsible for maintaining the user's balance and for reporting the purchase of the add-on as fulfilled to the Store after the user has consumed all the items. 使用者必須等到您的應用程式將先前的附加元件購買回報為已完全交付之後,才能再次購買該附加元件。The user cannot purchase the add-on again until your app has reported the previous add-on purchase as fulfilled.

例如,如果您的附加元件在遊戲中代表 100 個金幣,而使用者花費了 10 個金幣,則您的應用程式或服務必須針對該使用者保留 90 個金幣的新餘額。For example, if your add-on represents 100 coins in a game and the user consumes 10 coins, your app or service must maintain the new remaining balance of 90 coins for the user. 當使用者花光 100 個金幣之後,您的 App 必須回報該附加元件已完成,接著使用者就能再次購買 100 個金幣的附加元件。After the user has consumed all 100 coins, your app must report the add-on as fulfilled, and then the user can purchase the 100 coin add-on again.

Microsoft Store 管理的消費性產品Store-managed consumable 隨時可供購買、使用,然後再次購買的附加元件。An add-on that can be purchased, used, and purchased again at any time. Microsoft Store 會記錄使用者在該附加元件所代表項目的餘額。The Store keeps track of the user's balance of items that the add-on represents.

當使用者取用任何與附加元件相關的項目時,您必須負責向 Microsoft Store 回報這些項目已完成,而 Microsoft Store 會更新使用者的餘額。When the user consumes any items that are associated with the add-on, you are responsible for reporting those items as fulfilled to the Store, and the Store updates the user's balance. 使用者可以隨時多次購買附加元件 (不需要先取用項目)。The user can purchase the add-on as many times as they want (they do not need to consume the items first). 您的應用程式可以隨時查詢使用者目前的餘額。Your app can query for the current balance for the user at any time.

例如,如果您的附加元件在遊戲中代表最初的 100 個金幣數量,而使用者花費了 50 個金幣,則您的應用程式會向 Microsoft Store 回報已完成附加元件的 50 個單位,而 Microsoft Store 會更新剩下的餘額。For example, if your add-on represents an initial quantity of 100 coins in a game and the user consumes 50 coins, your app reports to the Store that 50 units of the add-on were fulfilled, and the Store updates the remaining balance. 如果使用者然後重新購買附加元件以獲取多 100 個硬幣,它們現在總計會有 150 個硬幣。If the user then repurchases your add-on to acquire 100 more coins, they will now have 150 coins total.

Note   注意  若要使用商店管理的耗材,您的應用程式必須以Windows 10 周年版為目標 (10.0;組建 14393) 或 Visual Studio 的較新版本,而且必須使用ApplicationModel命名空間, 而不是使用 windows. store 命名空間。Note  To use Store-managed consumables, your app must target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio, and it must use the Windows.Services.Store namespace instead of the Windows.ApplicationModel.Store namespace.

訂用帳戶Subscription 耐久性附加元件會繼續向客戶收取週期性費用,才能繼續使用附加元件。A durable add-on where the customer continues to be charged at recurring intervals in order to keep using the add-on. 客戶隨時都可以取消訂閱,避免進一步的費用。The customer can cancel the subscription at any time to avoid further charges.

Note   注意  若要使用訂用帳戶附加元件,您的應用程式必須以Windows 10 周年版為目標 (10.0;組建 14393) 或 Visual Studio 的較新版本,而且必須使用ApplicationModel命名空間, 而不是使用 windows. store 命名空間。Note  To use subscription add-ons, your app must target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio, and it must use the Windows.Services.Store namespace instead of the Windows.ApplicationModel.Store namespace.

注意

其他類型的附加元件 (例如,具有套件的耐久性附加元件,也稱為可下載內容或 DLC) 僅可供一組有限的開發人員使用,而本文件中並未涵蓋這個部分。Other types of add-ons, such as durable add-ons with packages (also known as downloadable content or DLC) are only available to a restricted set of developers, and are not covered in this documentation.

使用 Windows.Services.Store 命名空間的應用程式內購買和試用版In-app purchases and trials using the Windows.Services.Store namespace

本章節概略說明 Windows.Services.Store 命名空間的重要任務和概念。This section provides an overview of important tasks and concepts for the Windows.Services.Store namespace. 此命名空間僅提供給目標為 Visual Studio 中 Windows 10 Anniversary Edition (10.0;組建 14393) 或更新版本的應用程式 (這對應到 Windows 10 (版本 1607))。This namespace is available only to apps that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio (this corresponds to Windows 10, version 1607). 建議應用程式盡可能使用 Windows.Services.Store 命名空間,而不是 Windows.ApplicationModel.Store 命名空間。We recommend that apps use the Windows.Services.Store namespace instead of the Windows.ApplicationModel.Store namespace if possible. 如需 Windows.ApplicationModel.Store 命名空間的相關資訊,請參閱本文For information about the Windows.ApplicationModel.Store namespace, see this article.

本節內容In this section

影片Video

觀看下列影片,了解如何使用 Windows.Services.Store 命名空間,在您的應用程式中實作在應用程式內購買的概觀。Watch the following video for an overview of how to implement in-app purchases in your app using the Windows.Services.Store namespace.

開始使用 StoreContext 類別Get started with the StoreContext class

Windows.Services.Store 命名空間的主要進入點是 StoreContext 類別。The main entry point to the Windows.Services.Store namespace is the StoreContext class. 這個類別會提供一些方法,可供您用來取得目前應用程式及其可用附加元件的資訊、取得目前應用程式或其附加元件的授權資訊、為目前的使用者購買應用程式或附加元件,以及執行其他工作。This class provides methods you can use to get info for the current app and its available add-ons, get license info for the current app or its add-ons, purchase an app or add-on for the current user, and perform other tasks. 若要取得 StoreContext 物件,請執行下列其中一項操作︰To get a StoreContext object, do one of the following:

  • 在單一使用者應用程式 (也就是只在啟動應用程式的使用者內容中執行的應用程式),使用靜態 GetDefault 方法來取得 StoreContext 物件,您可以用來為使用者存取 Microsoft Store 相關的資料。In a single-user app (that is, an app that runs only in the context of the user that launched the app), use the static GetDefault method to get a StoreContext object that you can use to access Microsoft Store-related data for the user. 大多數通用 Windows 平台 (UWP) 應用程式都是單一使用者應用程式。Most Universal Windows Platform (UWP) apps are single-user apps.

    Windows.Services.Store.StoreContext context = StoreContext.GetDefault();
    
  • 多使用者應用程式 中,請使用靜態 GetForUser 方法來取得 StoreContext 物件,您可以針對在使用應用程式時使用其 Microsoft 帳戶登入的特定使用者,使用此物件來存取其 Microsoft Store 相關資料。In a multi-user app, use the static GetForUser method to get a StoreContext object that you can use to access Microsoft Store-related data for a specific user who is signed in with their Microsoft account while using the app. 下列範例會針對第一位可用的使用者取得 StoreContext 物件。The following example gets a StoreContext object for the first available user.

    var users = await Windows.System.User.FindAllAsync();
    Windows.Services.Store.StoreContext context = StoreContext.GetForUser(users[0]);
    

注意

使用傳統型橋接器的 Windows 傳統型應用程式必須先執行額外的步驟來設定 StoreContext 物件,才能使用此物件。Windows desktop applications that use the Desktop Bridge must perform additional steps to configure the StoreContext object before they can use this object. 如需詳細資訊,請參閱本節For more information, see this section.

在您具備 StoreContext 物件之後,您可以開始呼叫此物件的方法來取得目前應用程式及其附加元件的「市集」產品資訊、擷取目前應用程式及其附加元件的授權資訊、為目前的使用者購買應用程式或附加元件,以及執行其他工作。After you have a StoreContext object, you can start calling methods of this object to get Store product info for the current app and its add-ons, retrieve license info for the current app and its add-ons, purchase an app or add-on for the current user, and perform other tasks. 如需有關您可以使用此物件來執行常見工作的詳細資訊,請參閱下列文章:For more information about common tasks you can perform using this object, see the following articles:

如需示範如何在 Windows.Services.Store 命名空間中使用 StoreContext 和其他類型的範例應用程式,請參閱市集範例For a sample app that demonstrates how to use StoreContext and other types in the Windows.Services.Store namespace, see the Store sample.

實作應用程式內購買Implement in-app purchases

使用 Windows.Services.Store 命名空間在您的應用程式中為客戶提供應用程式內購買:To offer an in-app purchase to customers in your app using the Windows.Services.Store namespace:

  1. 如果您的應用程式提供客戶可購買的附加元件,請 在合作夥伴中心中為您的應用程式建立附加元件提交 If your app offers add-ons that customers can purchase, create add-on submissions for your app in Partner Center .

  2. 請在您的應用程式中撰寫程式碼以擷取您應用程式或您應用程式所提供之附加元件的產品資訊,然後判斷授權是否有效 (亦即,使用者是否具有可使用應用程式或附加元件的授權)。Write code in your app to retrieve product info for your app or an add-on offered by your app and then determine whether the license is active (that is, whether the user has a license to use the app or add-on). 如果授權無效,請顯示一個 UI 來以應用程式內購買形式為使用者提供要銷售的應用程式或附加元件。If the license isn't active, display a UI that offers the app or add-on for sale to the user as an in-app purchase.

  3. 如果使用者選擇購買您的應用程式或附加元件,請使用適當的方法來購買該產品:If the user chooses to purchase your app or add-on, use the appropriate method to purchase the product:

  4. 依照本文中的測試指導方針來測試您的實作。Test your implementation by following the testing guidance in this article.

實作試用版功能Implement trial functionality

使用 Windows.Services.Store 命名空間在您的應用程式試用版中排除或限制功能:To exclude or limit features in a trial version of your app using the Windows.Services.Store namespace:

  1. 將您的應用程式設定為合作夥伴中心中的免費試用版Configure your app as a free trial in Partner Center.

  2. 在您的應用程式中撰寫程式碼以擷取您應用程式或您應用程式所提供之附加元件的產品資訊,然後判斷與該應用程式關聯的授權是否為試用版授權Write code in your app to retrieve product info for your app or an add-on offered by your app and then determine whether the license associated with the app is a trial license.

  3. 如果是試用版,請在您的應用程式中排除或限制特定功能,然後再於使用者購買完整版授權時啟用這些功能。Exclude or limit certain features in your app if it is a trial, and then enable the features when the user purchases a full license. 如需詳細資訊和程式碼範例,請參閱實作應用程式的試用版For more information and a code example, see Implement a trial version of your app.

  4. 依照本文中的測試指導方針來測試您的實作。Test your implementation by following the testing guidance in this article.

測試您的應用程式內購買或試用實作。Test your in-app purchase or trial implementation

如果您的應用程式使用 Windows.Services.Store 命名空間中的 API 來實作 App 內購買或試用版功能,您必須將應用程式發行至 Microsoft Store,並將應用程式下載至您的開發裝置以便使用它的授權來進行測試。If your app uses APIs in the Windows.Services.Store namespace to implement in-app purchase or trial functionality, you must publish your app to the Store and download the app to your development device to use its license for testing. 請依照此程序,測試您的程式碼:Follow this process to test your code:

  1. 如果您的應用程式尚未在存放區中發佈和提供,請確定您的應用程式符合最低 Windows 應用程式認證套件 需求、在合作夥伴中心中 提交您的應用程式 ,並確定您的應用程式通過認證流程。If your app is not yet published and available in the Store, make sure your app meets the minimum Windows App Certification Kit requirements, submit your app in Partner Center, and make sure your app passes the certification process. 測試時您可以將應用程式設定為不可在市集中搜尋You can configure your app so it is not discoverable in the Store while you test it. 請注意 封裝航班的設定是否正確。Please note the proper configuration of package flights. 可能無法下載無法正確設定的套件航班。Incorrectly configured package flights may be not be able to be downloaded.

  2. 接著,確定您已完成下列操作:Next, make sure you have completed the following:

  3. 在 Visual Studio 中開啟您的專案,按一下 [ 專案] 功能表,指向 [ 儲存],然後按一下 [將應用程式與存放區建立關聯]。With your project open in Visual Studio, click the Project menu, point to Store, and then click Associate App with the Store. 完成 wizard 中的指示,將應用程式專案與您想要用於測試的合作夥伴中心帳戶中的應用程式產生關聯。Complete the instructions in the wizard to associate the app project with the app in your Partner Center account that you want to use for testing.

    注意

    如果您不會將專案與市集中的應用程式建立關聯,StoreContext 方法就會將其傳回值的 ExtendedError 屬性設定為錯誤碼值 0x803F6107。If you do not associate your project with an app in the Store, the StoreContext methods set the ExtendedError property of their return values to the error code value 0x803F6107. 這個值表示市集沒有任何關於該應用程式的知識。This value indicates that the Store doesn't have any knowledge about the app.

  4. 如果您尚未執行此動作,從市集中安裝您在上一個步驟指定的應用程式、執行一次應用程式,然後關閉此應用程式。If you have not done so already, install the app from the Store that you specified in the previous step, run the app once, and then close this app. 這可確保應用程式的有效授權已安裝於您的開發裝置上。This ensures that a valid license for the app is installed to your development device.

  5. 在 Visual Studio 中,開始執行或偵錯您的專案。In Visual Studio, start running or debugging your project. 您的程式碼應該從與您本機專案相關聯的市集應用程式中擷取應用程式和附加元件資料。Your code should retrieve app and add-on data from the Store app that you associated with your local project. 如果系統提示您重新安裝應用程式,請依照下列指示進行,然後執行或偵錯您的專案。If you are prompted to reinstall the app, follow the instructions and then run or debug your project.

    注意

    在您完成這些步驟之後,您可以繼續更新您的應用程式的程式碼,然後在您的開發電腦上偵錯更新的專案,而不用提交新的應用程式套件到市集。After you complete these steps, you can continue to update your app's code and then debug your updated project on your development computer without submitting new app packages to the Store. 您只需要下載您的應用程式的市集版到您的開發電腦,即可取得將用於測試的本機授權。You only need to download the Store version of your app to your development computer once to obtain the local license that will be used for testing. 您只需要在完成測試後提交新的應用程式套件到市集,並讓您的客戶能夠使用您的應用程式中的應用程式內購買或試用相關功能。You only need to submit new app packages to the Store after you complete your testing and you want to make the in-app purchase or trial-related features in your app available to your customers.

如果您的應用程式使用 Windows.ApplicationModel.Store 命名空間,您可在應用程式中使用 CurrentAppSimulator 類別,以便在提交 App 至市集之前的測試期間模擬授權資訊。If your app uses the Windows.ApplicationModel.Store namespace, you can use the CurrentAppSimulator class in your app to simulate license info during testing before you submit your app to the Store. 如需詳細資訊,請參閱 開始使用 CurrentApp 和 CurrentAppSimulator 類別For more information, see Get started with the CurrentApp and CurrentAppSimulator classes.

注意

Windows.Services.Store 命名空間不提供您可在測試期間用來模擬授權資訊的類別。The Windows.Services.Store namespace does not provide a class that you can use to simulate license info during testing. 如果您使用 Windows.Services.Store 命名空間來實作在應用程式中購買項目或試用版,您必須將應用程式發行至市集,並將應用程式下載至您的開發裝置以便使用它的授權來進行測試,如上所述。If you use the Windows.Services.Store namespace to implement in-app purchases or trials, you must publish your app to the Store and download the app to your development device to use its license for testing as described above.

應用程式內購買的收據Receipts for in-app purchases

Windows.Services.Store 命名空間不提供您可用來在應用程式程式碼中取得成功購買交易收據的 API。The Windows.Services.Store namespace does not provide an API you can use to obtain a transaction receipt for successful purchases in your app's code. 這與使用 Windows.ApplicationModel.Store 命名空間的應用程式是不同的體驗,這些應用程式可以使用用戶端 API 來擷取交易收據This is a different experience from apps that use the Windows.ApplicationModel.Store namespace, which can use a client-side API to retrieve a transaction receipt.

如果您使用 Windows.Services.Store 命名空間來實作 App 內購買,而您想要驗證指定的使用者是否已購買應用程式或附加元件,則您可以使用 Microsoft Store 集合 REST API 中的查詢產品方法If you implement in-app purchases using the Windows.Services.Store namespace and you want to validate whether a given customer has purchased an app or add-on, you can use the query for products method in the Microsoft Store collection REST API. 此方法的傳回資料會確認指定的客戶是否具備所指定產品的權益,並提供使用者取得該產品的交易資料。The return data for this method confirms whether the specified customer has an entitlement for a given product, and provides data for the transaction in which the user acquired the product. Microsoft Store 集合 API 會使用 Azure AD 驗證來擷取此資訊。The Microsoft Store collection API uses Azure AD authentication to retrieve this information.

使用 StoreContext 類別和傳統型橋接器Using the StoreContext class with the Desktop Bridge

使用傳統型橋接器的傳統型應用程式可以使用 StoreContext 類別來實作應用程式內購買和試用版。Desktop applications that use the Desktop Bridge can use the StoreContext class to implement in-app purchases and trials. 不過,如果您有 Win32 傳統型應用程式,或是有具備與轉譯架構 (例如 WPF 應用程式) 關聯之視窗控制代碼 (HWND) 的傳統型應用程式,您的應用程式就必須設定 StoreContext 物件,以指定哪一個應用程式視窗是物件所顯示的強制回應對話方塊的主控視窗。However, if you have a Win32 desktop application or a desktop application that has a window handle (HWND) that is associated with the rendering framework (such as a WPF application), your application must configure the StoreContext object to specify which application window is the owner window for modal dialogs that are shown by the object.

許多 StoreContext 成員 (以及透過 StoreContext 物件存取的其他相關類型的成員) 都會針對市集相關作業 (例如購買產品) 對使用者顯示強制回應對話方塊。Many StoreContext members (and members of other related types that are accessed through the StoreContext object) display a modal dialog to the user for Store-related operations such as purchasing a product. 如果傳統型應用程式未設定 StoreContext 物件來指定強制回應對話方塊的主控視窗,此物件將會傳回不正確的資料或錯誤。If a desktop application does not configure the StoreContext object to specify the owner window for modal dialogs, this object will return inaccurate data or errors.

若要在使用「傳統型橋接器」的傳統型應用程式中使用 StoreContext 物件,請依照下列步驟。To configure a StoreContext object in a desktop application that uses the Desktop Bridge, follow these steps.

  1. 執行下列其中一項,來讓您的應用程式存取 IInitializeWithWindow 介面:Do one of the following to enable your app to access the IInitializeWithWindow interface:

    • 如果您的應用程式是以受管理的語言 (例如 C# 或 Visual Basic) 撰寫的,請在您的應用程式程式碼中以 ComImport 屬性宣告 IInitializeWithWindow 介面,如以下 C# 範例所示。If your application is written in a managed language such as C# or Visual Basic, declare the IInitializeWithWindow interface in your app's code with the ComImport attribute as shown in the following C# example. 此範例假設您的程式碼檔案具有system.runtime.interopservices.outattribute命名空間的using語句。This example assumes that your code file has a using statement for the System.Runtime.InteropServices namespace.

      [ComImport]
      [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
      [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      public interface IInitializeWithWindow
      {
          void Initialize(IntPtr hwnd);
      }
      
    • 如果您的應用程式是以 C++ 撰寫,請在程式碼中新增對 shobjidl.h 標頭檔案的參考。If your application is written in C++, add a reference to the shobjidl.h header file in your code. 此標頭檔案包含 IInitializeWithWindow 介面的宣告。This header file contains the declaration of the IInitializeWithWindow interface.

  2. 使用 GetDefault 方法 (或 GetForUser,若您的應用程式是多使用者 App) 來取得 StoreContext 物件 (如本文稍早所述),然後將此物件轉換為 IInitializeWithWindow 物件。Get a StoreContext object by using the GetDefault method (or GetForUser if your app is a multi-user app) as described earlier in this article, and cast this object to an IInitializeWithWindow object. 接著,呼叫 IInitializeWithWindow.Initialize 方法,並傳遞要作為 StoreContext 方法所顯示任何強制回應對話方塊之擁有者的視窗控制代碼。Then, call the IInitializeWithWindow.Initialize method, and pass the handle of the window that you want to be the owner for any modal dialogs that are shown by StoreContext methods. 下列 C# 範例說明如何將您應用程式主要視窗的控制代碼傳遞給方法。The following C# example shows how to pass the handle of your app's main window to the method.

    StoreContext context = StoreContext.GetDefault();
    IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)context;
    initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
    

產品、SKU 和可用性Products, SKUs, and availabilities

「市集」中的每個產品都至少有一個 SKU,而每個 SKU 都至少會有一個「可用性」**。Every product in the Store has at least one SKU, and each SKU has at least one availability. 這些概念與大部分的開發人員在合作夥伴中心中都是抽象的,大部分的開發人員永遠都不會為其應用程式或附加元件定義 Sku 或可用性。These concepts are abstracted away from most developers in Partner Center, and most developers will never define SKUs or availabilities for their apps or add-ons. 不過,由於 Windows.Services.Store 命名空間中適用於市集產品的物件模型包含了 SKU 和可用性,因此對這些概念有基本了解在某些情況下是很有幫助的。However, because the object model for Store products in the Windows.Services.Store namespace includes SKUs and availabilities, a basic understanding of these concepts can be helpful for some scenarios.

ObjectObject 描述Description
ProductsProduct 產品是指市集中提供的任何類型產品,包括應用程式或附加元件。A product refers to any type of product that is available in the Store, including an app or add-on.

市集中每個產品都有對應的 StoreProduct 物件。Each product in the Store has a corresponding StoreProduct object. 此類別提供可讓您用來存取資料 (例如產品的「市集識別碼」、「市集」清單的影像和視訊,以及定價資訊) 的屬性。This class provides properties you can use to access data such as the Store ID of the product, the images and videos for the Store listing, and pricing info. 它也提供可讓您用來購買產品的方法。It also provides methods you can use to purchase the product.

SKUSKU SKU是特定版本的產品,具有自己的描述、價格和其他獨特的產品詳細資料。A SKU is a specific version of a product with its own description, price, and other unique product details. 每個應用程式或附加元件都有預設的 SKU。Each app or add-on has a default SKU. 大多數開發人員會對一個應用程式提供多個 SKU 的唯一時刻是在其發行完整版的應用程式和試用版時 (在市集型錄中,這其中每一個版本都是相同應用程式的不同 SKU)。The only time most developers will ever have multiple SKUs for an app is if they publish a full version of their app and a trial version (in the Store catalog, each of these versions is a different SKU of the same app).

某些發行者可以定義自己的 SKU。Some publishers have the ability to define their own SKUs. 例如,大型遊戲發行者可能在不允許紅色血液的市場中使用一個顯示綠色血液的 SKU 來發佈遊戲,並在所有其他市場中使用另一個顯示紅色血液的 SKU 來發佈。For example, a large game publisher might release a game with one SKU that shows green blood in markets that don't allow red blood and a different SKU that shows red blood in all other markets. 或者,銷售數位視訊內容的發行者可能針對某個視訊發佈兩個 SKU,一個 SKU 適用於高解析度版本,而另一個 SKU 適用於標準畫質版本。Alternatively, a publisher who sells digital video content might publish two SKUs for a video, one SKU for the high-definition version and a different SKU for the standard-definition version.

市集中每個 SKU 都有對應的 StoreSku 物件。Each SKU in the Store has a corresponding StoreSku object. 每個 StoreProduct 都有 Skus 屬性,可讓您用來存取產品的 SKU。Every StoreProduct has a Skus property you can use to access the SKUs for the product.

可用性Availability 可用性是特定版本的 SKU,具有自己獨特的定價資訊。An availability is a specific version of a SKU with its own unique pricing info. 每個 SKU 都有預設的可用性。Each SKU has a default availability. 某些發行者能夠定義自己的可用性,來介紹指定 SKU 的不同價格選項。Some publishers have the ability to define their own availabilities to introduce different price options for a given SKU.

市集中每個可用性都有對應的 StoreAvailability 物件。Each availability in the Store has a corresponding StoreAvailability object. 每個 StoreSku 都有 Availabilities 屬性,可讓您用來存取 SKU 的可用性。Every StoreSku has an Availabilities property you can use to access the availabilities for the SKU. 對於大部分開發人員而言,每個 SKU 都會有一個預設的可用性。For most developers, each SKU has a single default availability.

市集識別碼Store IDs

市集中每個應用程式、附加元件或其他產品都有相關的市集識別碼 (這有時候也稱為產品市集識別碼)。Every app, add-on, or other product in the Store has an associated Store ID (this is also sometimes called a product Store ID). 許多 API 需要有市集識別碼,才能在應用程式或附加元件上執行操作。Many APIs require the Store ID in order to perform an operation on an app or add-on.

「市集」中任何產品的「市集識別碼」都是 12 個字元的英數字串,例如 9NBLGGH4R315The Store ID of any product in the Store is 12-character alpha-numeric string, such as 9NBLGGH4R315. 有幾種不同方式可在市集中取得產品的市集識別碼:There are several different ways to get the Store ID for a product in the Store:

  • 針對應用程式,您可以在合作夥伴中心的 應用程式身分識別頁面 上取得 Store 識別碼。For an app, you can get the Store ID on the App identity page in Partner Center.
  • 針對附加元件,您可以在合作夥伴中心的附加元件的 [總覽] 頁面上取得 Store 識別碼。For an add-on, you can get the Store ID on the add-on's overview page in Partner Center.
  • 對於任何產品,您也可以使用代表產品之 StoreProduct 物件的 StoreId 屬性,以程式設計方式取得市集識別碼。For any product, you can also get the Store ID programmatically by using the StoreId property of the StoreProduct object that represents the product.

對於具有 SKU 和可用性的產品,SKU 和可用性也有自己的市集識別碼,但是格式不同。For products with SKUs and availabilities, the SKUs and availabilities also have their own Store IDs with different formats.

物件Object 市集識別碼格式Store ID format
SKUSKU SKU 的「市集識別碼」的格式為 <product Store ID>/xxxx,其中 xxxx 是 4 個字元的英數字串,用來識別產品的 SKU。The Store ID for a SKU has the format <product Store ID>/xxxx, where xxxx is a 4-character alpha-numeric string that identifies a SKU for the product. 例如: 9NBLGGH4R315/000NFor example, 9NBLGGH4R315/000N. 此識別碼是由StoreSku物件的StoreId屬性所傳回,有時也稱為SKU Store 識別碼This ID is returned by the StoreId property of a StoreSku object, and it is sometimes called the SKU Store ID.
可用性Availability 可用性的「市集識別碼」的格式為 <product Store ID>/xxxx/yyyyyyyyyyyy,其中 xxxx 是 4 個字元的英數字串,用來識別產品的 SKU,而 yyyyyyyyyyyy 是 12 個字元的英數字串,用來識別 SKU 的可用性。The Store ID for an availability has the format <product Store ID>/xxxx/yyyyyyyyyyyy, where xxxx is a 4-character alpha-numeric string that identifies a SKU for the product and yyyyyyyyyyyy is a 12-character alpha-numeric string that identifies an availability for the SKU. 例如: 9NBLGGH4R315/000N/4KW6QZD2VN6XFor example, 9NBLGGH4R315/000N/4KW6QZD2VN6X. 這個識別碼是由StoreAvailability物件的StoreId屬性所傳回,有時也稱為「可用性 Store 識別碼」。This ID is returned by the StoreId property of a StoreAvailability object, and it is sometimes called the availability Store ID.

如何在程式碼中使用附加元件的產品識別碼How to use product IDs for add-ons in your code

如果您想要讓客戶在應用程式的環境中使用附加元件,則當您在合作夥伴中心中建立附加元件提交時,必須輸入附加元件的唯一產品識別碼If you want to make an add-on available to your customers in the context of your app, you must enter a unique product ID for your add-on when you create your add-on submission in Partner Center. 您可以在程式碼中使用此產品識別碼來參考附加元件,不過您可使用產品識別碼的具體情況取決於您在應用程式中用於在應用程式內購買的命名空間。You can use this product ID to refer to the add-on in your code, although the specific scenarios in which you can use the product ID depend on which namespace you use for in-app purchases in your app.

注意

您在合作夥伴中心針對附加元件輸入的產品識別碼,與附加元件的 Store 識別碼不同。The product ID that you enter in Partner Center for an add-on is different than the add-on's Store ID. Store 識別碼是由合作夥伴中心所產生。The Store ID is generated by Partner Center.

使用 Windows.Services.Store 命名空間的應用程式Apps that use the Windows.Services.Store namespace

如果您的應用程式使用 Windows.Services.Store 命名空間,您可以使用產品識別碼輕鬆地找出代表附加元件的 StoreProduct,或代表附加元件授權的 StoreLicenseIf your app uses the Windows.Services.Store namespace, you can use the product ID to easily identify the StoreProduct that represents your add-on or the StoreLicense that represents your add-on's license. 產品識別碼由 StoreProduct.InAppOfferTokenStoreLicense.InAppOfferToken 屬性公開。The product ID is exposed by the StoreProduct.InAppOfferToken and StoreLicense.InAppOfferToken properties.

注意

雖然產品識別碼是在程式碼中識別附加元件的有用方法,但 Windows.Services.Store 命名空間中的大部分操作使用附加元件的市集識別碼,而不是產品識別碼。Although the product ID is a useful way to identify an add-on in your code, most operations in the Windows.Services.Store namespace use the Store ID of an add-on instead of the product ID. 例如,以程式設計方式取得應用程式的一或多個已知附加元件,傳遞附加元件的市集識別碼 (而非產品識別碼) 到 GetStoreProductsAsync 方法。For example, to programmatically retrieve one or more known add-ons for an app, pass the Store IDs (rather than the product IDs) of the add-ons to the GetStoreProductsAsync method. 同樣地,在消費性附加元件完成時報告、傳遞附加元件的市集識別碼 (而非產品識別碼) 到 ReportConsumableFulfillmentAsync 方法。Similarly, to report a consumable add-on as fulfilled, pass the Store ID of the add-on (rather than the product ID) to the ReportConsumableFulfillmentAsync method.

使用 Windows.ApplicationModel.Store 命名空間的應用程式Apps that use the Windows.ApplicationModel.Store namespace

如果您的應用程式使用 ApplicationModel 命名空間,您必須使用您在合作夥伴中心中指派給附加元件的產品識別碼,以進行大部分的作業。If your app uses the Windows.ApplicationModel.Store namespace, you'll need to use the product ID that you assign to an add-on in Partner Center for most operations. 例如:For example: