次の方法で共有


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

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

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

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

[前提条件]

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

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

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

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

デスクトップ ブリッジを使用するデスクトップ アプリケーションがある場合は、これらの例に示されていないコードを追加して、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}";
}

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

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

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

次の使用例は、現在のアプリに関連付けられている指定されたストア 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...
    }
}

現在のアプリから購入できるアドオンの情報を取得する

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

アプリに購入可能なアドオンが多数ある場合は、GetAssociatedStoreProductsWithPagingAsync メソッドを使用して、ページングを用いてアドオンの結果を取得できます。

次の例では、現在のアプリから購入できる、すべての永続的なアドオン、ストアで管理される消耗品アドオン、および開発者が管理する消耗品アドオンの情報を取得します。

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

ユーザーが購入した現在のアプリのアドオンの情報を取得する

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

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

次の例では、指定した ストア 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...
    }
}