创建资源词典,首先新建两个词典文件en-us.xaml、zh-cn.xaml
zh-cn.xaml:
1
2
3
4
5
6
7
8
9<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GPIO_TOOL_WIN"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String x:Key="buttonTest1">测试按钮1</s:String>
<s:String x:Key="buttonTest2">测试按钮2</s:String>
<s:String x:Key="buttoncn">中文</s:String>
<s:String x:Key="buttonen">英文</s:String>
</ResourceDictionary>en-us.xaml:
1
2
3
4
5
6
7
8
9<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TEST_TOOL_WIN"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String x:Key="buttonTest1">Button TEST1</s:String>
<s:String x:Key="buttonTest2">Button TEST2</s:String>
<s:String x:Key="buttoncn">Chinese</s:String>
<s:String x:Key="buttonen">English</s:String>
</ResourceDictionary>将两个资源词典添加到App.xaml中
1
2
3
4
5
6
7
8
9
10
11
12
13
14<Application x:Class="GPIO_TOOL_WIN.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TEST_TOOL_WIN"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\en-us.xaml" />
<ResourceDictionary Source="Resources\zh-cn.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>在界面设计器中需要显示的位置添加动态资源,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<Window x:Class="GPIO_TOOL_WIN.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TEST_TOOL_WIN"
mc:Ignorable="d"
Title="WPF TEST" Height="450" Width="800">
<Grid>
<Button x:Name="buttonTest1" Content="{DynamicResource buttonTest1}" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="100"/>
<Button x:Name="buttonTest2" Content="{DynamicResource buttonTest2}" HorizontalAlignment="Left" Margin="20,50,0,0" VerticalAlignment="Top" Width="100"/>
<Button x:Name="buttoncn" Content="{DynamicResource buttoncn}" HorizontalAlignment="Left" Margin="20,150,0,0" VerticalAlignment="Top" Width="75" Click="Buttoncn_Click"/>
<Button x:Name="buttonen" Content="{DynamicResource buttonen}" HorizontalAlignment="Left" Margin="120,150,0,0" VerticalAlignment="Top" Width="75" Click="Buttonen_Click"/>
</Grid>
</Window>切换语言代码如下:
1
2
3
4
5
6
7
8
9List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
{
dictionaryList.Add(dictionary);
}
string requestedCulture = @"Resources\en-us.xaml";
ResourceDictionary resourceDictionary = dictionaryList.Find(d => d.Source.OriginalString.Equals(requestedCulture));
Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);测试效果如下图: