从代码更新 Store 发布的应用Update Store-published apps from your code

从 Windows 10 版本1607开始,版本 (版本 14393) ,Windows 10 允许开发人员更好地保证应用商店中的应用更新。Starting in Windows 10, version 1607 (build 14393), Windows 10 allows developers to make stronger guarantees around app updates from the Store. 执行此操作需要几个简单的 Api,创建一致且可预测的用户体验,并使开发人员能够专注于他们最能完成的工作,同时允许 Windows 执行繁重的工作。Doing this requires a few simple APIs, creates a consistent and predictable user experience and lets developers to focus on what they do best while allowing Windows to do the heavy lifting.

可以通过两种基本方式管理应用更新。There are two fundamental ways that app updates can be managed. 在这两种情况下,这些方法的最终结果相同-应用更新。In both cases, the net result for these methods is the same - the update is applied. 但是,在一种情况下,您可以选择让系统完成所有工作,而在另一种情况下,您可能希望更深入地控制用户体验。However, in one case, you can choose to let the system do all the work while in the other case you might want to have a deeper level of control over the user experience.

简单更新Simple updates

首先,最重要的是非常简单的 API 调用,告诉系统检查更新、下载更新,然后请求用户提供安装权限。First and foremost is the very simple API call that tells the system to check for updates, download them and then request permission from the user to install them. 首先,使用 StoreContext 类获取 StorePackageUpdate 对象,然后下载并安装它们。You'll start by using the StoreContext class to get StorePackageUpdate objects, download and install them.

using Windows.Services.Store;

private async void GetEasyUpdates()
{
    StoreContext updateManager = StoreContext.GetDefault();
    IReadOnlyList<StorePackageUpdate> updates = await updateManager.GetAppAndOptionalStorePackageUpdatesAsync();

    if (updates.Count > 0)
    {
        IAsyncOperationWithProgress<StorePackageUpdateResult, StorePackageUpdateStatus> downloadOperation = 
            updateManager.RequestDownloadAndInstallStorePackageUpdatesAsync(updates);
        StorePackageUpdateResult result = await downloadOperation.AsTask();
    }
}

此时,用户有两个选项可供选择:立即应用更新或推迟更新。At this point the user has two options they can choose from: apply the update now or defer the update. 用户所做的任何选择都将通过对象返回到, StorePackageUpdateResult 使开发人员可以执行更多操作,例如关闭应用程序(如果需要继续更新或只是稍后重试)。Whatever choice the user makes will be returned back to via the StorePackageUpdateResult object allowing developers to take further actions such as closing down the app if the update is required to continue or simply trying again later.

精细控制的更新Fine-controlled updates

对于希望获得完全自定义体验的开发人员,提供了额外的 Api,可对更新过程进行更多控制。For developers who want to have a completely customized experience, additional APIs are provided which enable more control over the update process. 平台使你能够执行以下操作:The platform enables you to do the following:

  • 获取单个包下载或整个更新上的进度事件。Get progress events on an individual package download or on the whole update.
  • 在用户和应用的方便而不是其中的一个上应用更新。Apply updates at the user's and app's convenience rather than one or the other.

开发人员可以在后台下载更新 (当应用程序正在使用) 然后请求用户安装更新时,如果用户拒绝,你可以只禁用受更新影响的功能(如果你选择)。Developers are able to download updates in the background (while app is in use) then request the user install updates, if they decline, you can simply disable capabilities affected by the update if you choose.

下载更新Download updates

private async void DownloadUpdatesAsync()
{
    StoreContext updateManager = StoreContext.GetDefault();
    IReadOnlyList<StorePackageUpdate> updates = await updateManager.GetAppAndOptionalStorePackageUpdatesAsync();

    if (updates.Count > 0)
    {
        IAsyncOperationWithProgress<StorePackageUpdateResult, StorePackageUpdateStatus> downloadOperation =
            updateManager.RequestDownloadStorePackageUpdatesAsync(updates);

        downloadOperation.Progress = async (asyncInfo, progress) =>
        {
            // Show progress UI
        };

        StorePackageUpdateResult result = await downloadOperation.AsTask();
        if (result.OverallState == StorePackageUpdateState.Completed)
        {
            // Update was downloaded, add logic to request install
        }
    }
}

安装 更新Install updates

private async void InstallUpdatesAsync()
{
    StoreContext updateManager = StoreContext.GetDefault();
    IReadOnlyList<StorePackageUpdate> updates = await updateManager.GetAppAndOptionalStorePackageUpdatesAsync();    

    // Save app state here

    IAsyncOperationWithProgress<StorePackageUpdateResult, StorePackageUpdateStatus> installOperation =
        updateManager.RequestDownloadAndInstallStorePackageUpdatesAsync(updates);

    StorePackageUpdateResult result = await installOperation.AsTask();

    // Under normal circumstances, app will terminate here

    // Handle error cases here using StorePackageUpdateResult from above
}

强制执行更新Making updates mandatory

在某些情况下,可能需要将必须安装到用户设备的更新,使其真正是必需的 (例如,对无法等待) 的应用的关键修复。In some cases, it might actually be desirable to have an update that must be installed to a user's device - making it truly mandatory (e.g. a critical fix to an app that can't wait). 在这些情况下,可以采取其他措施来强制更新。In these cases, there are additional measures that you can take to make the update mandatory.

  1. 在应用程序代码中实现必需的更新逻辑 (需要在强制更新自身) 之前执行。Implement the mandatory update logic in your app code (would need to be done before mandatory update itself).
  2. 在提交到开发人员中心的过程中,请确保选中 " 使此更新成为必需 的" 框。During submission to the Dev Center, ensure the Make this update mandatory box is selected.

实现应用代码Implementing app code

为了充分利用必需的更新,你需要对上面的代码进行一些细微的修改。In order to take full advantage of mandatory updates, you'll need to make some slight modifications to the code above. 需要使用 StorePackageUpdate 对象 来确定更新是否是必需的。You'll need to use the StorePackageUpdate object to determine if the update is mandatory.

 private async bool CheckForMandatoryUpdates()
{
    StoreContext updateManager = StoreContext.GetDefault();
    IReadOnlyList<StorePackageUpdate> updates = await updateManager.GetAppAndOptionalStorePackageUpdatesAsync();

    if (updates.Count > 0)
    {
        foreach (StorePackageUpdate u in updates)
        {
            if (u.Mandatory)
                return true;
        }
    }
    return false;
}

然后,需要创建一个自定义的应用程序对话框,以通知用户有必需的更新,并且必须安装它以继续充分利用应用。Then you'll need to create a custom in app dialog to inform the user that there is a mandatory update and that they must install it to continue full use of the app. 如果用户拒绝更新,应用程序可能会降低功能 (例如,阻止联机访问) 或完全终止 (例如仅联机游戏) 。If the user declines the update, the app could either degrade functionality (for example, prevent online access) or terminate completely (for example, online-only games).

合作伙伴中心Partner Center

若要确保 StorePackageUpdate 对强制更新显示 true,则需要在 " " 页面的 "合作伙伴中心" 中将更新标记为必需。To ensure the StorePackageUpdate shows true for a mandatory update, you will need to mark the update as mandatory in the Partner Center in the Packages page.

请注意以下几点:A couple of things to note:

  • 如果在强制的更新与另一个非强制更新取代后设备恢复联机状态,则在该设备强制执行之前,非必需的更新仍会在设备上显示为必需的。If a device comes back online after a mandatory update has been superseded with another non-mandatory update, the non-mandatory update will still show up on the device as mandatory given the missed update before it was mandatory.
  • 开发人员控制的更新和强制更新当前仅限于应用商店。Developer-controlled updates and mandatory updates are currently limited to the Store.