WPF 实现语言切换

 源码下载:LanguageStyle.zip

 

第一步:创建language文件夹,并创建文件(zh-cn.xaml、en-us.xaml)

 zh-cn.xaml  en-us.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="确认">confirm</sys:String>
    <sys:String x:Key="取消">cancel</sys:String>
    <sys:String x:Key="删除">delete</sys:String>
    <sys:String x:Key="警告">warning</sys:String>
    <sys:String x:Key="提醒">info</sys:String>
    <sys:String x:Key="成功">success</sys:String>
</ResourceDictionary>

 

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="确认">确认</sys:String>
    <sys:String x:Key="取消">取消</sys:String>
    <sys:String x:Key="删除">删除</sys:String>
    <sys:String x:Key="警告">警告</sys:String>
    <sys:String x:Key="提醒">提醒</sys:String>
    <sys:String x:Key="成功">成功</sys:String>
</ResourceDictionary>

 

 

第二步:修改配置文件(App.config)

<appSettings>
  <add key="language" value="en-us"/>
</appSettings>

 

 第三步:App启动资源文件

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        // 获取配置
        if(ConfigurationManager.AppSettings["language"] != null)
        {
            if(!String.IsNullOrEmpty(ConfigurationManager.AppSettings["language"]))
            {
                var requestedLanguage = String.Format("pack://application:,,,/;component/language/{0}.xaml", ConfigurationManager.AppSettings["language"]);
                var resource = Application.Current.Resources.MergedDictionaries.FirstOrDefault(rd => rd.Source != null && rd.Source.OriginalString.ToLower().Contains("language"));
                if(resource == null)
                {
                    ResourceDictionary resourceDictionary = new ResourceDictionary();
                    resourceDictionary.Source = new Uri(requestedLanguage, UriKind.RelativeOrAbsolute);
                    Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                }
            }
        }
    }
    protected override void OnExit(ExitEventArgs e)
    {
        // 在这里处理应用程序退出逻辑
        base.OnExit(e);
    }
}

 

 第四步:选择并保存默认语言

public void SetLanguage(string language = "zh-cn")
{
    if (string.IsNullOrEmpty(language)) language = "zh-cn";
    // 获取当前语言
    var resourceDictionary = Application.Current.Resources.MergedDictionaries.FirstOrDefault(rd => rd.Source != null
    && rd.Source.OriginalString.ToLower().Contains("language"));
    if (resourceDictionary != null)
    {
        //移除语言
        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);

        //添加语言
        string requestedLanguage = $"pack://application:,,,/;component/language/{language}.xaml";
        resourceDictionary.Source = new Uri(requestedLanguage, UriKind.RelativeOrAbsolute);
        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

        // 保存配置
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.Settings["language"].Value = language;
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

        MessageBox.Show("保存成功");
    }
}

 

posted @ 2024-05-28 23:52  microsoft-zhcn  阅读(67)  评论(0编辑  收藏  举报