WPF程序国际化

1.创建相应的xaml文件

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sp="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">


    <sp:ImageSource x:Key="Logo">
        logo url
    </sp:ImageSource>

    <sys:String x:Key="Msg">
       string
    </sys:String>

</ResourceDictionary>

2.配置App.xaml

<Application.Resources>
<ResourceDictionary>
<!-- 这个节点就是配置默认语言的 -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Languagesen_US.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

3.程序中如果要修改,则可以使用如下代码

                ResourceDictionary langRd = null;
                try
                {
                    //base on language config file
                    langRd = Application.LoadComponent(new Uri(@"Languages" + languageName + ".xaml", UriKind.Relative)) as ResourceDictionary;
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message);
                }

                if (langRd != null)
                {
                    //clear base style
                    //if (this.Resources.MergedDictionaries.Count > 0)
                    //{
                    //    this.Resources.MergedDictionaries.Clear();
                    //}
                    this.Resources.MergedDictionaries.Add(langRd);
                }

 4.如果要单独取得某个配置项目,则可以在程序中使用如下代码:

using System.Windows
(string)TryFindResource("Msg");
原文地址:https://www.cnblogs.com/amwuau/p/7513997.html