資源套件Resource Packages

資源套件提供了一種絕佳的方式,可透過將語言分割為個別的套件,或將特定資產調整為個別套件(視使用者的電腦設定而定),以減少使用者磁片使用量。Resource packages offers a great way to reduce users disk footprint by segmenting language or scale specific asset into separate packages that are downloaded automatically by Windows depending on the users machine configuration. 如果使用者將新的語言新增至其區域中的作業系統語言清單 & 語言 設定,或變更顯示設定,則在自動存放區更新之間,OS 將會提取適用于裝置上所有已安裝應用程式的資源套件。If the user adds a new language to their list of OS languages in Region & language settings or changes the display configuration, across an automatic store update, the OS will fetch applicable resource packages for all installed apps on the device.

在 Windows SDK 10.0.17095.0 ADDRESOURCEPACKAGEASYNC API 可讓開發人員視需要安裝應用程式的資源套件。In Windows SDK 10.0.17095.0 AddResourcePackageAsync API allows developers to install a resource package for an app on demand.

如何使用 AddResourcePackageAsync APIHow to use the AddResourcePackageAsync API

  • AddResourcePackageAsync 會取得應用程式的 PackageFamilyName ,以及可唯一識別嘗試下載之資源套件的資源識別碼。AddResourcePackageAsync takes the PackageFamilyName of the application and the resource ID that uniquely identifies the resource package that is trying to download. 資源識別碼會對應至資源套件AppxManifest.xml中的ResourceId元素。The resource ID corresponds to the ResourceId element in the AppxManifest.xml of the resource package.

  • 應用程式必須重新開機,以取得應用程式可用之所有資源的合併視圖。The application must restart to get a merged view of all resources that are available to the application. API 提供可傳遞以重新開機應用程式的 ForceTargetApplicationShutdown 選項。The API offers the ForceTargetApplicationShutdown option that can pass to restart the application.

  • 如果在下載資源套件時,應用程式有可用的更新,則 API 將會失敗,因為繼承應用程式無法再使用。If there is an update available for the application when the resource package is being downloaded, the API will fail as the older version of the app is no longer available. 藉由傳入 ApplyUpdateIfAvailable 旗標,API 將會更新應用程式,並在相同的下載過程中取得要求的資源套件。By passing in the ApplyUpdateIfAvailable flag, the API will update the app as well as get the requested resource package as part of the same download.

  • API 會傳回可顯示在應用程式中的下載進度。The API returns a progress for the download that can display in the application. 另請注意,針對發佈至存放區的應用程式,資源套件取得會顯示在 [Microsoft Store 下載和更新 ] 頁面中,並顯示為應用程式的更新。Also note that for apps published to the store, the resource package acquisition shows up in the Microsoft Store downloads and updates page and shows up as an update to your application. 終端使用者可以選擇暫停或取消從 Microsoft Store 下載資源套件。The end user can choose to pause or cancel the download of the resource package from the Microsoft Store.

範例Example

 private async void DownloadGermanResourcePackage(object sender, RoutedEventArgs e)
{            
    // ResourceId that is unique to the resource package
    string resourceId = "split.language-de";
    // Warn user that application will need to restart.
    // To take update if available pass in the ApplyUpdateIfAvailable flag
    var packageCatalog = PackageCatalog.OpenForCurrentPackage();
    PackageCatalogAddResourcePackageResult result = await packageCatalog.AddResourcePackageAsync("29270depappf.CaffeMacchiato_gah1vdar1nn7a", resourceId, 
        AddResourcePackageOptions.ApplyUpdateIfAvailable | AddResourcePackageOptions.ForceTargetApplicationShutdown)
        .AsTask<PackageCatalogAddResourcePackageResult, PackageInstallProgress>(new Progress
        (progress =>;
        {
                // Draw progress
        }));

        if (result.ExtendedError != null)
        {
                //Display error or retry if needed
        }
}

驗證Validation

針對本機驗證,開發人員可以建立 msixbundle 或 .appxbundle,然後從您的本機磁片磁碟機、網路共用或 web 伺服器進行安裝。For local validation, developers can create an .msixbundle or .appxbundle and install from your local drive, network share or webserver. 當應用程式呼叫 AddResourcePackageAsync API 時,Windows 會從安裝原始應用程式的位置取得資源套件。When the app calls the AddResourcePackageAsync API, Windows will acquire the resource package from the location where the original application was installed. 這可簡化驗證。This makes the validation simple. 這項工作完成後,就可以開始部署應用程式。Once this works, the app is ready to be deployed.

移除資源套件Removing resource packages

應用程式可以選擇移除,以移除 (s 的資源套件,) 透過 REMOVERESOURCEPACKAGEASYNC API下載的資源套件。The app can choose to remove to remove the resource package(s) it downloaded via the RemoveResourcePackageAsync API. 不過,無法卸載適用于使用者或裝置的資源套件。However, Resource packages that are applicable for the user or the device as a whole cannot be uninstalled.

範例Example

 private async void RemoveResourcePackage(object sender, RoutedEventArgs e)
{            
    var packageCatalog = PackageCatalog.OpenForCurrentPackage();
    List resourcePackagesToRemove = new List();
    resourcePackagesToRemove.Add("split.language-de");
    //Warn user that application will be restarted
    var removePackageResult = await packageCatalog.RemoveResourcePackagesAsync(resourcePackagesToRemove);
    if (removePackageResult.ExtendedError != null)
    {
        // display error
    }
}