Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Utilize StoreContext in Windows Applications

StoreContext is a class provided by the Windows SDK that allows developers to interact with the Microsoft Store. It is particularly useful for managing in-app purchases, retrieving product information, and handling license validation. Understanding how to use StoreContext can significantly enhance the functionality of your Windows applications, especially if they rely on monetization through the Microsoft Store.

In the context of Windows, StoreContext is used within UWP (Universal Windows Platform) applications. This article will guide you through the process of setting up and using StoreContext in your Windows applications, providing practical examples to illustrate its usage.

Examples:

  1. Setting Up StoreContext in a UWP Application

    To use StoreContext, you first need to include the necessary namespaces and initialize the StoreContext object.

    using Windows.Services.Store;
    
    public sealed partial class MainPage : Page
    {
       StoreContext context = null;
    
       public MainPage()
       {
           this.InitializeComponent();
           context = StoreContext.GetDefault();
       }
    }
  2. Retrieving Product Information

    You can retrieve information about products available in the Microsoft Store using the StoreContext object. Here’s how you can do it:

    private async void GetProductInfo()
    {
       StoreProductQueryResult result = await context.GetStoreProductsAsync(new string[] { "Durable" }, new string[] { "product1", "product2" });
    
       if (result.ExtendedError != null)
       {
           // Handle error
           return;
       }
    
       foreach (var item in result.Products)
       {
           StoreProduct product = item.Value;
           // Use product information
           Debug.WriteLine($"Product: {product.Title}, Price: {product.Price.FormattedPrice}");
       }
    }
  3. Handling In-App Purchases

    StoreContext also allows you to handle in-app purchases. Below is an example of how to initiate a purchase:

    private async void BuyProduct(string storeId)
    {
       StorePurchaseResult result = await context.RequestPurchaseAsync(storeId);
    
       switch (result.Status)
       {
           case StorePurchaseStatus.Succeeded:
               // Handle successful purchase
               break;
           case StorePurchaseStatus.AlreadyPurchased:
               // Handle already purchased case
               break;
           case StorePurchaseStatus.NotPurchased:
               // Handle not purchased case
               break;
           case StorePurchaseStatus.NetworkError:
           case StorePurchaseStatus.ServerError:
               // Handle network or server error
               break;
       }
    }
  4. License Validation

    Validating the license of your app or its add-ons is crucial for ensuring that only authorized users can access certain features. Here’s an example of how to check the license status:

    private async void CheckLicense()
    {
       StoreAppLicense appLicense = await context.GetAppLicenseAsync();
    
       if (appLicense.IsActive)
       {
           // License is active
           if (appLicense.IsTrial)
           {
               // App is in trial mode
           }
           else
           {
               // App is fully licensed
           }
       }
       else
       {
           // License is not active
       }
    }

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.