UWP: experience the StoreContext class and uwpstorecontext, a new interface for In-APP purchase

Source: Internet
Author: User

UWP: experience the StoreContext class and uwpstorecontext, a new interface for In-APP purchase

After Windows 1607 (internal version 14393), Microsoft added some new interfaces related to the app store in the SDK, such as application trial and purchase, and in-APP purchase. These interfaces are much more convenient than the original interfaces. In terms of in-APP purchase, the previous method was: to facilitate testing during development, add an xml file to the project, which describes the product information in the application, use CurrentAppSimulator to complete related operations. When the application is to be published to the store, replace all CurrentAppSimulator with CurrentApp. When the new interface is used, it can be directly in the development environment, you can access the products we added in the Developer Center (dev.windows.com/dashboard), which is exactly the same as the actual purchase operations, so the new interface is more convenient and intuitive.

The new interface information is as follows:

Namespace: Windows. Services. Store class: StoreContext

Here is a Demo:

1. first, create a new application in the Windows Developer Center as the application for this test, create several add-ons in it, and set the corresponding title and price, the type of the add-on created in this example is Durable.

2. Next, create a UWP Project. For TargetVersion and MinVersion, select Windows Aniversary Edtion (10.0, Build 14393 ). After the project is created, right-click the project and choose Store> Associate app with the Store. In the displayed window, associate this project with the application just created in the Developer Center.

3. Place the following XAML content in MainPage. xaml:

Private readonly StoreContext storeContext = StoreContext. getDefault (); public MainPage () {this. initializeComponent (); Loaded + = MainPage_Loaded;} private async void listProducts_ItemClick (object sender, ItemClickEventArgs e) {StoreContext context = StoreContext. getDefault (); var product = e. clickedItem as StoreProduct; var result = await context. requestPurchaseAsync (product. storeId); if (result. status = StorePurchaseStatus. succeeded) {// successful purchase} else if (result. status = StorePurchaseStatus. alreadyPurchased) {// already purchased} else if (result. status = StorePurchaseStatus. notPurchased) {// The user has not purchased the service, that is, the user has canceled the operation} else if (result. status = StorePurchaseStatus. serverError | result. status = StorePurchaseStatus. networkError) {// error} private async void MainPage_Loaded (object sender, Windows. UI. xaml. routedEventArgs e) {// The GetAssociatedStoreProductsAsync method is used to obtain the list of products that can be purchased in the current application // it accepts a string list type parameter, which indicates the Product type (Product Kind) the list is equivalent to a filtering condition // For the product type, the SDK currently provides the following: // Application, Game, Consumable, UnmanagedConsumable, Durable;
// Consumable and Durable are commonly used; in code, we need to obtain all Durable (permanent) type of product // For details about ProductKind, refer:
// Https://docs.microsoft.com/en-us/uwp/api/windows.services.store.storeproduct#Windows_Services_Store_StoreProduct_ProductKind
Var result = await storeContext. GetAssociatedStoreProductsAsync (new string [] {"Durable"}); if (result. ExtendedError! = Null) {// error} else {// list all Durable items and display StringBuilder sb = new StringBuilder (); var products = result. products. values; foreach (var item in products) {sb. appendLine ($ "{item. title}-{item. storeId}-{item. inAppOfferToken}-{item. productKind}-{item. price. formattedPrice} ");} await new MessageDialog (sb. toString (), string. empty ). showAsync (); this. listProducts. itemsSource = products ;}}

Compile and run the product. After obtaining the purchased products from the store, the app will pop up a dialog box showing the information of these purchased products. In the list on the page, click a certain item to purchase the product. This process is the same as the actual purchase operation, and it is the real purchase process. Therefore, using the new API is as simple as this.

Since the above Code already has detailed comments, I will not explain the Code any more. If you want to learn more about the StoreContext class and related information, refer:

StoreContext Class

ProductKind

In-APP purchase and trial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.