可以使用卫星程序集为多个区域性配置 Windows 窗体应用程序。 卫星程序集是一个程序集,其中包含非应用程序默认区域性文化的应用程序资源。
如 本地化 ClickOnce 应用程序中所述,可以在同一 ClickOnce 部署中包含多个卫星程序集,用于多个区域性。 默认情况下,ClickOnce 会将部署中的所有附属程序集下载到客户端计算机,尽管单个客户端可能只需要一个附属程序集。
本演练演示如何将附属程序集标记为可选,并仅下载客户端计算机对其当前区域性设置所需的程序集。
注释
ApplicationDeployment 类和System.Deployment.Application 命名空间中的 API 在 .NET Core 和 .NET 5 及更高版本中不受支持。 在 .NET 7 中,支持访问应用程序部署属性的新方法。 有关详细信息,请参阅 .NET 中的 Access ClickOnce 部署属性。 .NET 7 不支持 ApplicationDeployment 方法的等效项。
注释
出于测试目的,以下代码示例以编程方式将文化设置为 ja-JP。 有关如何为生产环境调整此代码的信息,请参阅本主题后面的“后续步骤”部分。
将附属程序集标记为可选
- 生成项目。 这将为您要本地化到的所有文化生成卫星程序集。 
- 在解决方案资源管理器中右键单击项目名称,然后单击“ 属性”。 
- 单击“ 发布 ”选项卡,然后单击“ 应用程序文件”。 
- 选中“ 显示所有文件 ”复选框以显示卫星程序集。 默认情况下,所有卫星程序集都将包含在部署中,并在此对话框中可见。 - 附属程序集的名称 <格式为 isoCode>\ApplicationName.resources.dll,其中 <isoCode> 是 RFC 1766 格式的语言标识符。 
- 单击每个语言标识符的“下载组”列表中的“新建”。 当系统提示输入下载组名称时,请输入语言标识符。 例如,对于日本卫星组装,您可以指定下载组名称 - ja-JP。
- 关闭 “应用程序文件 ”对话框。 
在 C# 中按需下载附属程序集
- 打开 Program.cs 文件。 如果在解决方案资源管理器中看不到此文件,请选择项目,然后在 “项目 ”菜单上,单击“ 显示所有文件”。 
- 使用以下代码下载相应的附属程序集并启动应用程序。 - using System; using System.Collections.Generic; using System.Windows.Forms; using System.Threading; using System.Globalization; using System.Deployment.Application; using System.Reflection; namespace ClickOnce.SatelliteAssemblies { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP"); // Call this before initializing the main form, which will cause the resource manager // to look for the appropriate satellite assembly. GetSatelliteAssemblies(Thread.CurrentThread.CurrentCulture.ToString()); Application.Run(new Form1()); } static void GetSatelliteAssemblies(string groupName) { if (ApplicationDeployment.IsNetworkDeployed) { ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment; if (deploy.IsFirstRun) { try { deploy.DownloadFileGroup(groupName); } catch (DeploymentException de) { // Log error. Do not report this error to the user, because a satellite // assembly may not exist if the user's culture and the application's // default culture match. } } } } } }
在 Visual Basic 中按需下载卫星程序集
- 在应用程序的 “属性” 窗口中,单击 “应用程序 ”选项卡。 
- 在选项卡页底部,单击“ 查看应用程序事件”。 
- 将以下导入添加到 ApplicationEvents.VB 文件的开头。 - Imports System.Deployment.Application Imports System.Globalization Imports System.Threading
- 将以下代码添加到 - MyApplication类。- Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup Thread.CurrentThread.CurrentUICulture = New CultureInfo("ja-JP") GetSatelliteAssemblies(Thread.CurrentThread.CurrentUICulture.ToString()) End Sub Private Shared Sub GetSatelliteAssemblies(ByVal groupName As String) If (ApplicationDeployment.IsNetworkDeployed) Then Dim deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment If (deploy.IsFirstRun) Then Try deploy.DownloadFileGroup(groupName) Catch de As DeploymentException ' Log error. Do not report this error to the user, because a satellite ' assembly may not exist if the user's culture and the application's ' default culture match. End Try End If End If End Sub
后续步骤
在生产环境中,您可能需要删除代码示例中将 CurrentUICulture 设置为特定值的那一行,因为客户端计算机默认会自动设置正确的值。 例如,当您的应用程序在日语客户端计算机上运行时,CurrentUICulture 默认将为 ja-JP。 以编程方式设置它是在部署应用程序之前测试卫星程序集的好方法。