アプリとアドオンの製品情報の取得

この記事では、Windows.Services.Store 名前空間の StoreContext クラスのメソッドを使って、現在のアプリとそのアドオンの Microsoft Store に関連する情報を取得する方法について説明します。

完全なサンプル アプリケーションについては、ストア サンプルをご覧ください。

注意

Windows.Services.Store 名前空間は、Windows 10 バージョン 1607 で導入され、Visual Studio で、Windows 10 Anniversary Edition (10.0、ビルド 14393) 以降のリリースをターゲットとするプロジェクトでのみ使用できます。 アプリが Windows 10 の以前のバージョンをターゲットする場合、Windows.Services.Store 名前空間の代わりに Windows.ApplicationModel.Store 名前空間を使う必要があります。 詳細については、 こちらの記事を参照してください。

前提条件

これらの例には、次の前提条件があります。

  • Windows 10 Anniversary Edition (10.0、ビルド 14393) 以降のリリースをターゲットとするユニバーサル Windows プラットフォーム (UWP) アプリの Visual Studio プロジェクト。
  • パートナー センターでアプリの申請を作成し、このアプリが Microsoft Store で公開されている。 必要に応じで、テスト中にストアでアプリを検索できないようにアプリを構成することも可能です。 詳しくは、テスト ガイダンスをご覧ください。
  • アプリのアドオンの製品情報を取得する場合、パートナー センターでアドオンを作成する必要もあります。

これらの例のコードは、次の点を前提としています。

  • コードは、workingProgressRing という名前の ProgressRingtextBlock という名前の TextBlock を含む Page のコンテキストで実行されます。 これらのオブジェクトは、それぞれ非同期操作が発生していることを示するためと、出力メッセージを表示するために使用されます。
  • コード ファイルには、Windows.Services.Store 名前空間の using ステートメントがあります。
  • アプリは、アプリを起動したユーザーのコンテキストでのみ動作するシングル ユーザー アプリです。 詳しくは、「アプリ内購入と試用版」をご覧ください。

注意

デスクトップ ブリッジを使用するデスクトップ アプリケーションがある場合、これらの例には示されていないコードを追加して StoreContext オブジェクトを構成することが必要になることがあります。 詳しくは、「デスクトップ ブリッジを使用するデスクトップ アプリケーションでの StoreContext クラスの使用」をご覧ください。

現在のアプリの情報の取得

現在のアプリに関するストア製品情報を取得するには、GetStoreProductForCurrentAppAsync メソッドを使います。 これは、価格などの情報の取得に使うことができる StoreProduct オブジェクトを返す非同期メソッドです。

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}";
}

現在のアプリに関連付けられている既知の Store ID を持つアドオンの情報の取得

現在のアプリに関連付けられていて、既に Store ID がわかっているアドオンの Store 製品情報を取得するには、GetStoreProductsAsync メソッドを使います。 これは、各アドオンを表す StoreProduct オブジェクトのコレクションを返す非同期メソッドです。 ストア ID に加えて、アドオンの種類を識別する文字列の一覧をこのメソッドに渡す必要があります。 サポートされている文字列値の一覧についてはProductKind プロパティをご覧ください。

注意

GetStoreProductsAsync メソッドは、アドオンが現在購入可能かどうかにかかわらず、アプリに関連付けられている指定のアドオンの製品情報を返します。 現在のアプリで現在購入可能なすべてのアドオンの情報を取得するには、代わりに次のセクションで説明する GetAssociatedStoreProductsAsync メソッドを使います。

次の例では、現在のアプリに関連付けられていて、指定された Store ID を持つ永続的なアドオンの情報を取得します。

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...
    }
}

現在のアプリから購入可能なアドオンの情報の取得

現在のアプリから現在購入可能なアドオンの Store 製品情報を取得するには、GetAssociatedStoreProductsAsync メソッドを使います。 これは、利用可能な各アドオンを表す StoreProduct オブジェクトのコレクションを返す非同期メソッドです。 取得するアドオンの種類を識別する文字列の一覧をこのメソッドに渡す必要があります。 サポートされている文字列値の一覧についてはProductKind プロパティをご覧ください。

注意

アプリから購入可能なアドオンの数が多い場合は、代わりに GetAssociatedStoreProductsWithPagingAsync メソッドを使って、ページングを利用してアドオンの結果を返すこともできます。

次の例では、現在のアプリから購入できる、すべての永続的なアドオン、Store で管理されるコンシューマブルなアドオン、開発者により管理されるコンシューマブルなアドオンの情報を取得します。

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...
    }
}

現在のアプリでユーザーが購入済みのアドオンの情報の取得

現在のユーザーが購入したアドオンの Store 製品情報を取得するには、GetUserCollectionAsync メソッドを使います。 これは、各アドオンを表す StoreProduct オブジェクトのコレクションを返す非同期メソッドです。 取得するアドオンの種類を識別する文字列の一覧をこのメソッドに渡す必要があります。 サポートされている文字列値の一覧についてはProductKind プロパティをご覧ください。

注意

アプリのアドオンが多くある場合、代わりに GetUserCollectionWithPagingAsync メソッドを使ってページングを利用し、アドオンの結果を返すこともできます。

次の例では、指定された Store ID を持つ永続的なアドオンの情報を取得します。

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...
    }
}