获取应用和加载项的许可证信息Get license info for apps and add-ons

本文介绍了如何使用 Windows.Services.Store 命名空间中 StoreContext 类的方法,来获取当前应用及其加载项的许可证信息。This article demonstrates how to use methods of the StoreContext class in the Windows.Services.Store namespace to get license info for the current app and its add-ons. 例如,你可以使用此信息来确定应用或其加载项的许可证是否处于活动状态,或确定它们是否是试用许可证。For example, you can use this info to determine if the licenses for the app or its add-ons are active, or if they are trial licenses.

备注

Windows.Services.Store 命名空间在 Windows 10 版本 1607 中引入,它仅可用于面向 Windows 10 周年纪念版(10.0;版本 14393)或 Visual Studio 更高版本的项目中。The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. 如果你的应用面向 Windows 10 的较早版本,则必须使用 Windows.ApplicationModel.Store 命名空间来替代 Windows.Services.Store 命名空间。If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. 有关详细信息,请参阅此文For more information, see this article.

先决条件Prerequisites

本示例有以下先决条件:This example has the following prerequisites:

  • 适用于面向 Windows 10 周年纪念版(10.0;版本 14393)或更高版本的通用 Windows 平台 (UWP) 应用的 Visual Studio 项目。A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
  • 你有合作伙伴中心中的创建应用提交并在应用商店中发布此应用。You have created an app submission in Partner Center and this app is published in the Store. 在测试应用期间,你可以选择将应用配置为在应用商店中隐藏。You can optionally configure the app so it is not discoverable in the Store while you test it. 有关详细信息,请参阅我们的测试指南For more information, see our testing guidance.
  • 如果你想获取应用的加载项的许可证信息,还必须创建合作伙伴中心中的加载项If you want get license info for an add-on for the app, you must also create the add-on in Partner Center.

此示例中的代码假设:The code in this example assumes:

  • 代码在含有 ProgressRing(名为 workingProgressRing)和 TextBlock(名为 textBlock)的 Page 上下文中运行。The code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. 这些对象分别用于指示是否正在进行异步操作和显示输出消息。These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • 代码文件有一个适用于 Windows.Services.Store 命名空间的 using 语句。The code file has a using statement for the Windows.Services.Store namespace.
  • 该应用是单用户应用,仅在启动该应用的用户上下文中运行。The app is a single-user app that runs only in the context of the user that launched the app. 有关详细信息,请参阅应用内购买和试用For more information, see In-app purchases and trials.

备注

如果你有使用桌面桥的桌面应用程序,可能需要添加不在此示例中显示的额外代码来配置 StoreContext 对象。If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in this example to configure the StoreContext object. 有关详细信息,请参阅在使用桌面桥的桌面应用程序中使用 StoreContext 类For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

代码示例Code example

若要获取当前应用的许可证信息,请使用 GetAppLicenseAsync 方法。To get license info for the current app, use the GetAppLicenseAsync method. 这是一种异步方法,它返回提供应用的许可证信息的 StoreAppLicense 对象,其中包括指示用户当前是否具有使用应用的有效许可证的属性 (IsActive) 和指示许可证是否用于试用版的属性 (IsTrial)。This is an asynchronous method that returns a StoreAppLicense object that provides license info for the app, including properties that indicate whether the user currently has a valid license to use the app (IsActive) and whether the license is for a trial version (IsTrial).

若要访问用户有权使用的当前应用的持久性加载项的许可证,请使用 StoreAppLicense 对象的 AddOnLicenses 属性。To access the licenses for durable add-ons of the current app for which the user has an entitlement to use, use the AddOnLicenses property of the StoreAppLicense object. 此属性返回 StoreLicense 对象集合,表示加载项许可证。This property returns a collection of StoreLicense objects that represent the add-on licenses.

private StoreContext context = null;

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

    workingProgressRing.IsActive = true;
    StoreAppLicense appLicense = await context.GetAppLicenseAsync();
    workingProgressRing.IsActive = false;

    if (appLicense == null)
    {
        textBlock.Text = "An error occurred while retrieving the license.";
        return;
    }

    // Use members of the appLicense object to access license info...

    // Access the valid licenses for durable add-ons for this app.
    foreach (KeyValuePair<string, StoreLicense> item in appLicense.AddOnLicenses)
    {
        StoreLicense addOnLicense = item.Value;
        // Use members of the addOnLicense object to access license info
        // for the add-on.
    }
}

有关完整的应用程序示例,请参阅 Microsoft Store 示例For a complete sample application, see the Store sample.