资源包Resource Packages

资源包提供了一种很好的方法,可通过将语言分段或将特定的资产划分为 Windows 自动下载的单独包(具体取决于用户计算机配置)来减少用户的磁盘占用量。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 和唯一标识尝试下载的资源包的资源 ID。AddResourcePackageAsync takes the PackageFamilyName of the application and the resource ID that uniquely identifies the resource package that is trying to download. 资源 ID 对应于资源包的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

应用可选择删除以删除资源包, (通过 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
    }
}