透過您的程式碼更新存放區發佈的應用程式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.