Condividi tramite


Ottenere informazioni sul prodotto per app e componenti aggiuntivi

Questo articolo illustra come usare i metodi della classe StoreContext nello spazio dei nomi Windows.Services.Store per accedere alle informazioni correlate allo Store per l'app corrente o uno dei relativi componenti aggiuntivi.

Per un'applicazione di esempio completa, vedere l'esempio di Store.

Nota

Lo spazio dei nomi Windows.Services.Store è stato introdotto in Windows 10, versione 1607, e può essere usato solo nei progetti che hanno come target Windows 10 Anniversary Edition (10.0; Build 14393) o una versione successiva in Visual Studio. Se l'app è destinata a una versione precedente di Windows 10, devi usare lo spazio dei nomi Windows.ApplicationModel.Store anziché lo spazio dei nomi Windows.Services.Store. Per ulteriori informazioni, vedere questo articolo.

Prerequisiti

Questi esempi presentano i prerequisiti seguenti:

  • Un progetto di Visual Studio per un'app UWP (Universal Windows Platform) che ha come target Windows 10 Anniversary Edition (10.0; Build 14393) o una versione successiva.
  • Hai creato un invio di app nel Centro per i partner e questa app viene pubblicata nello Store. Facoltativamente, puoi configurare l'app in modo che non sia individuabile nello Store durante il test. Per altre informazioni, vedere le linee guida per i test .
  • Se vuoi ottenere informazioni sul prodotto per un componente aggiuntivo per l'app, devi anche creare il componente aggiuntivo nel Centro per i partner.

Il codice in questi esempi presuppone:

  • Il codice viene eseguito nel contesto di una Page che contiene un ProgressRing denominato workingProgressRing e un TextBlock denominato textBlock. Questi oggetti vengono usati per indicare che si sta verificando un'operazione asincrona e per visualizzare rispettivamente i messaggi di output.
  • Il file di codice include un usando'istruzione per lo spazio dei nomi Windows.Services.Store.
  • L'app è un'app a utente singolo che viene eseguita solo nel contesto dell'utente che ha avviato l'app. Per ulteriori informazioni, vedere acquisti in-app e prove gratuite.

Nota

Se si dispone di un'applicazione desktop che usa Desktop Bridge, potrebbe essere necessario aggiungere codice aggiuntivo non illustrato in questi esempi per configurare l'oggetto StoreContext. Per altre informazioni, vedere Uso della classe StoreContext in un'applicazione desktop che usa Desktop Bridge.

Ottenere informazioni per l'app corrente

Per ottenere informazioni sul prodotto dello Store relativo all'app corrente, usa il metodo GetStoreProductForCurrentAppAsync. Si tratta di un metodo asincrono che restituisce un oggetto StoreProduct che è possibile usare per ottenere informazioni come il prezzo.

private StoreContext context = null;

public async void GetAppInfo()
{
    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.
    }

    // Get app store product details. Because this might take several moments,   
    // display a ProgressRing during the operation.
    workingProgressRing.IsActive = true;
    StoreProductResult queryResult = await context.GetStoreProductForCurrentAppAsync();
    workingProgressRing.IsActive = false;

    if (queryResult.Product == null)
    {
        // The Store catalog returned an unexpected result.
        textBlock.Text = "Something went wrong, and the product was not returned.";

        // Show additional error info if it is available.
        if (queryResult.ExtendedError != null)
        {
            textBlock.Text += $"\nExtendedError: {queryResult.ExtendedError.Message}";
        }

        return;
    }

    // Display the price of the app.
    textBlock.Text = $"The price of this app is: {queryResult.Product.Price.FormattedBasePrice}";
}

Ottenere informazioni per i componenti aggiuntivi con ID dello Store noti associati all'app corrente

Per ottenere informazioni sul prodotto dello Store per i componenti aggiuntivi associati all'app corrente e per cui conosci già gli ID dello Store , usa il metodo GetStoreProductsAsync. Si tratta di un metodo asincrono che restituisce una raccolta di oggetti StoreProduct che rappresentano ognuno dei componenti aggiuntivi. Oltre agli ID dello Store, devi passare un elenco di stringhe a questo metodo che identifica i tipi dei componenti aggiuntivi. Per un elenco dei valori stringa supportati, vedere la proprietà ProductKind.

Nota

Il metodo GetStoreProductsAsync restituisce informazioni sul prodotto per i componenti aggiuntivi specificati associati all'app, indipendentemente dal fatto che i componenti aggiuntivi siano attualmente disponibili per l'acquisto. Per recuperare informazioni su tutti i componenti aggiuntivi attualmente acquistabili per l'app corrente, utilizzare il metodo GetAssociatedStoreProductsAsync come descritto nella sezione seguente.

In questo esempio vengono recuperate informazioni per i componenti aggiuntivi durevoli con gli ID dello Store specificati associati all'app corrente.

private StoreContext context = null;

public async void GetProductInfo()
{
    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.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    // Specify the Store IDs of the products to retrieve.
    string[] storeIds = new string[] { "9NBLGGH4TNMP", "9NBLGGH4TNMN" };

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult =
        await context.GetStoreProductsAsync(filterList, storeIds);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store info for the product.
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}

Ottenere informazioni sui componenti aggiuntivi disponibili per l'acquisto dall'app corrente

Per ottenere informazioni sul prodotto dello Store per i componenti aggiuntivi attualmente disponibili per l'acquisto dall'app corrente, usa il metodo GetAssociatedStoreProductsAsync. Si tratta di un metodo asincrono che restituisce un insieme di oggetti StoreProduct che rappresentano ognuno dei componenti aggiuntivi disponibili. È necessario passare un elenco di stringhe a questo metodo che identificano i tipi di componenti aggiuntivi che si desidera recuperare. Per un elenco dei valori stringa supportati, vedere la proprietà ProductKind.

Nota

Se l'app include molti componenti aggiuntivi disponibili per l'acquisto, puoi anche utilizzare il metodo GetAssociatedStoreProductsWithPagingAsync per utilizzare il paging e restituire i risultati dei componenti aggiuntivi.

L'esempio seguente recupera informazioni per tutti i componenti aggiuntivi durevoli, i componenti aggiuntivi di consumo gestiti dallo Store e i componenti aggiuntivi di consumo gestiti dallo sviluppatore disponibili per l'acquisto dall'app corrente.

private StoreContext context = null;

public async void GetAddOnInfo()
{
    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.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetAssociatedStoreProductsAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store product info for the add-on.
        StoreProduct product = item.Value;

        // Use members of the product object to access listing info for the add-on...
    }
}

Ottenere informazioni per i componenti aggiuntivi per l'app corrente acquistata dall'utente

Per ottenere informazioni sul prodotto Store per i componenti aggiuntivi acquistati dall'utente corrente, usare il metodo GetUserCollectionAsync. Si tratta di un metodo asincrono che restituisce una raccolta di oggetti StoreProduct che rappresentano ognuno dei componenti aggiuntivi. È necessario passare un elenco di stringhe a questo metodo che identificano i tipi di componenti aggiuntivi che si desidera recuperare. Per un elenco dei valori stringa supportati, vedere la proprietà ProductKind.

Nota

Se l'app include molti componenti aggiuntivi, in alternativa puoi usare il metodo GetUserCollectionWithPagingAsync per usare il paging per restituire i risultati del componente aggiuntivo.

Nell'esempio seguente vengono recuperate informazioni sui componenti aggiuntivi durevoli con gli ID dello Store specificati .

private StoreContext context = null;

public async void GetUserCollection()
{
    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.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetUserCollectionAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}