WPF -- 一种实现本地化的方法

本文介绍一种WPF程序实现本地化的方法。

步骤

首先,假设xaml文件中存在一个Button按钮,内容为“按钮”,实现本地化的步骤如下:

  1. 展开程序的Properties,双击Resources.resx文件进行编辑;
  2. 添加一条资源,名称为"btnContent",值为"按钮";
  3. 右键复制Resources.resx然后粘贴,改名为Resources.en-US.resx;
  4. 添加资源,名称为"btnContent",值为"button";
  5. 在需要使用该资源的xaml文件中,引入Properties名称空间:
xmlns:prop="clr-namespace:WpfApplication1.Properties"
  1. 使用方法:
<Button Content="{x:Static prop:Resources.btnContent}"/>
  1. 测试:
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
    base.OnStartup(e);
    // 注释掉该行会显示汉语,否则会显示英语
    System.Threading.Thread.CurrentThread.CurrentUICulture = 
        new System.Globalization.CultureInfo("en-US");
}
转载请注明出处,欢迎交流。
原文地址:https://www.cnblogs.com/louzixl/p/14298288.html