WPF多语言切换

          最近在用wpf做一个项目,需要提供中文和俄文切换,这是效果图

WPF多语言切换_第1张图片WPF多语言切换_第2张图片WPF多语言切换_第3张图片WPF多语言切换_第4张图片

1.在程序根目录下新建两个资源词典文件 ,注意加上:xmlns:sys="clr-namespace:System;assembly=mscorlib”

中文:“zh-CN.xaml”

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                     xmlns:local="clr-namespace:JNTApp">
   
    登录
    账号:
    密码:
    选择语言:
    记住密码
    自动登录
    登录
    取消
    中文
    俄文

  俄文:“ru.xaml”

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:local="clr-namespace:JNTApp">
   
    войти
    Логин:
    Пароль:
    выбор языка:
    запомнить Пароль
    автоматический вход
    войти
    отменить
    китайский язык
    русский язык

定义完成之后,在界面引用; Text="{DynamicResource txtAccount}"

2.在App.xaml加上


       
           
               
               
          

       

   

3.在App.xaml.cs加一个方法

 private void Application_Startup(object sender, StartupEventArgs e)
        {

            Application currApp = Application.Current;
            currApp.StartupUri = new Uri("SignWindow.xaml", UriKind.RelativeOrAbsolute);
//调用方法
            LoadLanguage();
        }
//加载语言
        private void LoadLanguage()
        {
            CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
            ResourceDictionary langRd = null;
            try
            {
                langRd =
                Application.LoadComponent(
                new Uri(currentCultureInfo.Name + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch
            {
            }
            if (langRd != null)
            {
                if (this.Resources.MergedDictionaries.Count > 0)
                {
                    this.Resources.MergedDictionaries.Clear();
                }
                this.Resources.MergedDictionaries.Add(langRd);
            }
        }

4.登录时,首先初始化默认是哪种语言,我这边默认的是中文

 public SignWindow()
        {
            InitializeComponent();
            currentLang = "zh-CN";
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");

        }

5.切换语言
   //登录成功后跳转到主界面
 private MainWindow MainWindow;

 ///
        /// 语言选择
        ///

        ///
        ///
       private void cobChoose_DropDownClosed(object sender, EventArgs e)
        {
            string language = this.cobChoose.Text;
            //语言类型
            string languageType = "";
            if (!string.IsNullOrEmpty(language))
            {
                switch (language)
                {
                    case "中文": languageType = "zh-CN"; break;
                    case "俄文": languageType = "ru"; break;
                    case "китайский язык": languageType = "zh-CN"; break;
                    case "русский язык": languageType = "ru"; break;

                }
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
                 //切换语言的方法
                ChangeLanguage.change(languageType, this);
                if (MainWindow != null)
                {
                    ChangeLanguage.change(languageType, MainWindow);
                }
                currentLang = languageType;
                 //定义一个session来保存当前选中的语言,供其它界面使用
                Variable.LanguageSeesion = currentLang;
            }
        }
   
6.切换语言的方法,定义一个ChangeLanguage类
 static  class ChangeLanguage
    {
     // 这个是window窗体
        public static  void change(string currentLang,Window w)
        {
          System.Windows.ResourceDictionary langRd = null;

            CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
            try
            {
                langRd =
               System.Windows.Application.LoadComponent(
                new Uri(currentLang + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch
            {
            }
            if (langRd != null)
            {
                if (w.Resources.MergedDictionaries.Count > 0)
                {
                    w.Resources.MergedDictionaries.Clear();
                }
                w.Resources.MergedDictionaries.Add(langRd);
            }

        }

//这个是page页面
        public static void changePage(string currentLang)
        {
            System.Windows.ResourceDictionary langRd = null;

            CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
            try
            {
                langRd =
               System.Windows.Application.LoadComponent(
                new Uri(currentLang + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch
            {
            }
            if (langRd != null)
            {
                Application.Current.Resources.MergedDictionaries.Clear();
                Application.Current.Resources.MergedDictionaries.Add(langRd);
             
            }

        }

    }

第一次写博客,只管贴代码,有些地方可能没说清楚,不过看代码应该能看清楚吧





你可能感兴趣的:(WPF多语言切换)