本文演示如何使用 Windows.Services.Store 命名空间中 StoreContext 类的方法获取当前应用及其加载项的许可证信息。 例如,可以使用此信息来确定应用或其加载项的许可证是否处于活动状态,或者它们是否为试用许可证。
注释
Windows.Services.Store 命名空间是在 Windows 10 版本 1607 中引入的,并且只能在面向 Windows 10 周年版(10.0; 版本 14393) 或更高版本的项目中使用,需在 Visual Studio 中构建。 如果你的应用面向早期版本的 Windows 10,则必须使用 Windows.ApplicationModel.Store 命名空间而不是 Windows.Services.Store 命名空间。 有关详细信息,请参阅 本文。
先决条件
此示例具有以下先决条件:
- 适用于面向 Windows 10 周年纪念版(10.0;版本 14393)或更高版本的通用 Windows 平台 (UWP) 应用的 Visual Studio 项目。
- 在合作伙伴中心创建应用提交,此应用在应用商店中发布。 可以选择配置应用,以便在测试应用时无法在应用商店中发现它。 有关详细信息,请参阅我们的 测试指南。
- 如果想要获取到应用程序插件的许可证信息,还必须 在合作伙伴中心创建插件。
此示例中的代码假定:
- 代码在 Page 上下文中运行,其中包含名为 的
workingProgressRing
,以及名为 的textBlock
。 这些对象分别用于指示异步操作的发生和显示输出消息。 - 代码文件中有一个 ,该文件使用 语句用于 Windows.Services.Store 命名空间.
- 该应用是一个单用户应用,仅在启动应用的用户的上下文中运行。 有关详细信息,请参阅 应用内购买和试用版。
注释
如果你有使用 Desktop Bridge的桌面应用程序,则可能需要添加本示例中未显示的其他代码来配置 StoreContext 对象。 有关详细信息,请参阅在使用桌面桥的桌面应用程序中使用的 StoreContext 类 。
代码示例
若要获取当前应用的许可证信息,请使用 GetAppLicenseAsync 方法。 这是一种异步方法,它返回一个 StoreAppLicense 对象,该对象提供应用的许可证信息,包括指示用户当前是否具有使用应用的有效许可证的属性(IsActive),以及许可证是否为试用版(IsTrial)。
若要访问用户有权使用的当前应用的持久加载项的许可证,请使用 StoreAppLicense 对象的 AddOnLicenses 属性。 此属性返回一个包含 StoreLicense 对象的集合,这些对象表示加载项许可证。
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.
}
}
有关完整的示例应用程序,请参阅 Store 示例。