C# 杂项一一测试界面实现中英文切换

我们在做项目的时候,经常会通过菜单栏来实现测试界面的中英文切换,如下所示:

当程序切换到中文时,界面显示中文,切换到英文时,界面显示英文

1.首先我们为菜单按钮添加响应事件:

private void SetMenuClickEvent()
{
	//option
	toolStripMenuItem_option_language_cn.Click += new System.EventHandler(Menu_Option_Click_language);
	toolStripMenuItem_option_language_en.Click += new System.EventHandler(Menu_Option_Click_language);
}

2.事件实现:根据不同的菜单选择不同的语言,并设置测试界面的语言

private void Menu_Option_Click_language(object sender, EventArgs e)
{
	ToolStripMenuItem item = sender as ToolStripMenuItem;
	ToolStripMenuItem[] arr = new ToolStripMenuItem[] { toolStripMenuItem_option_language_en,
									toolStripMenuItem_option_language_cn};
	System.Globalization.CultureInfo  cti = null;

	if (item == toolStripMenuItem_option_language_en)
	{
		SetMenuImage(arr, 0);
		cti = new System.Globalization.CultureInfo("en");
	}
	else if(item == toolStripMenuItem_option_language_cn)
	{
		SetMenuImage(arr, 1);
		cti = new System.Globalization.CultureInfo("zh-CHS");
	}
	else
	{
		return;
	}

	System.Threading.Thread.CurrentThread.CurrentCulture = cti;
	System.Threading.Thread.CurrentThread.CurrentUICulture = cti;

	SetLanguage();
}
private void SetLanguage()
{
	SetMenuLanguage();
	SetCtrlLanguage();
}
private void SetMenuLanguage()
{
	if (InvokeRequired)
	{
		BeginInvoke(new Action(SetMenuLanguage));
		return;
	}

	toolStripMenuItem_device.Text = Resources.ResourceManager.GetString("Menu_device");
	toolStripMenuItem_device_setting.Text = Resources.ResourceManager.GetString("Menu_device_setting");

	toolStripMenuItem_option.Text = Resources.ResourceManager.GetString("Menu_option");

	toolStripMenuItem_help.Text = Resources.ResourceManager.GetString("Menu_help");
	toolStripMenuItem_help_about.Text = Resources.ResourceManager.GetString("Menu_help_about");
	toolStripMenuItem_help_userGuid.Text = Resources.ResourceManager.GetString("Menu_help_userGuid");
	toolStripMenuItem_help_log.Text = Resources.ResourceManager.GetString("Menu_help_log");

	toolStripMenuItem_readFile.Text = Resources.ResourceManager.GetString("Menu_readFile");
	toolStripMenuItem_readFile_readCsv.Text = Resources.ResourceManager.GetString("Menu_readFile_readCsv");
}

上述,Resources是工程下的一个资源文件,我们在资源文件中添加对应的中英文对照字符串,如下所示:

当然,我们也可以通过获取其他Form上的Resources资源文件来实现中英文字符串的切换

private void SetCtrlLanguage()
{
	ResourceManager rm = new ResourceManager(typeof(Form_Gadgets));
	this.Text = rm.GetString("$this.Text");
	Ready.Text = rm.GetString("Ready.Text");
	tabPage_AuxMic.Text = rm.GetString("tabPage_AuxMic.Text");
	tabPage_Earpiece.Text = rm.GetString("tabPage_Earpiece.Text");
	tabPage_MainMic.Text = rm.GetString("tabPage_MainMic.Text");
	tabPage_Speaker.Text = rm.GetString("tabPage_Speaker.Text");
	tabPage_Vibrator.Text = rm.GetString("tabPage_Vibrator.Text");
}

这样,我们就可以通过切换菜单栏来实现界面中英文切换了。

  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现 WPF 界面内容的中英文切换功能,可以通过以下步骤实现: 1. 在工程中创建一个资源文件(.resx),用于存储中英文的字符串资源。 2. 在资源文件中添加中英文的字符串资源。例如,添加一个 Name 字符串资源,分别对应中文和英文的名称。 3. 在 WPF 界面中,使用 Binding 绑定字符串资源。例如,使用 {x:Static} 绑定 Name 字符串资源,如下所示: ```xml <TextBlock Text="{x:Static properties:Resources.Name}" /> ``` 其中,properties 表示资源文件的命名空间,Resources 表示资源文件名称。 4. 在代码中,通过修改资源文件的语言属性,实现中英文切换。例如,使用以下代码切换到中文: ```csharp Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); ``` 使用以下代码切换到英文: ```csharp Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); ``` 切换语言后,需要刷新界面,让界面重新加载资源文件中的字符串资源,使用以下代码刷新界面: ```csharp InitializeComponent(); this.DataContext = this; ``` 其中,this.DataContext = this; 表示重新绑定数据上下文。 完整代码示例: MainWindow.xaml: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:properties="clr-namespace:WpfApp1.Properties" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel> <TextBlock Text="{x:Static properties:Resources.Name}" /> <TextBlock Text="{x:Static properties:Resources.Description}" /> <Button Content="{x:Static properties:Resources.Button}" /> </StackPanel> </Grid> </Window> ``` MainWindow.xaml.cs: ```csharp using System.Globalization; using System.Threading; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = this; } private void SwitchLanguage_Click(object sender, RoutedEventArgs e) { if (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN") { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); } else { Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); } InitializeComponent(); this.DataContext = this; } } } ``` 注意:为了让资源文件的字符串资源在代码中能够被访问到,需要在资源文件的属性设置中将 Access Modifier 属性设置为 Public。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值