【6月日新计划17】WPF入门-ResXManager多語言切換
1. Official Website
官網地址: www.vsixgallery.com/extension/4…
1.下載安裝
可能是網絡不好,我是一直裝不上
2.Visual Studio 插件安裝
3.檢查是否安裝成功
2. UserGuide
2.1 Config
1.創建資源文件(ResourceFile.resx)
2.命名規則
點擊工具 -> ResX Manager -> Languages
按照第一步創建文件,命名有規律,語種參考上面的Languages:
- UiResource.resx
- UiResource.zh-CN.resx
- UiResource.ja-JP.resx
- UiResource.seh-MZ.resx
2.2 Translate
支持多種語言間的翻譯,還有多種翻譯器
翻譯後點擊右下角的選項,自動填充,非常Nice
2.3 Code
此時,你已經完成了2.1,創建了四個語言文件
1.創建LanguageResource
注意,是和文件在同級目錄
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Resources;
namespace BlankApp1.Resources.Languages
{
public class LanguageResource : INotifyPropertyChanged
{
private readonly ResourceManager _resourceManager;
private static readonly Lazy<LanguageResource> _lazy = new Lazy<LanguageResource>(() => new LanguageResource());
public static LanguageResource Instance => _lazy.Value;
public event PropertyChangedEventHandler PropertyChanged;
public LanguageResource()
{
// _resourceManager = new ComponentResourceManager(typeof(LanguageResource));
_resourceManager = new ResourceManager("BlankApp1.Resources.Languages.UiResource", typeof(LanguageResource).Assembly);
}
public string this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _resourceManager.GetString(name);
}
}
public void ChangeLanguage(CultureInfo cultureInfo)
{
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]")); //字符串集合,对应资源的值
}
}
}
2.創建View
<UserControl
x:Class="BlankApp1.Views.Config.ResxView"
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:language="clr-namespace:BlankApp1.Resources.Languages"
xmlns:local="clr-namespace:BlankApp1.Views.Config"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="130" />
<RowDefinition Height="5" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<WrapPanel>
<ComboBox
x:Name="LanguageList"
Width="130"
Height="45"
Margin="10"
materialDesign:HintAssist.Hint="Language"
FontSize="18"
FontWeight="Bold"
SelectedIndex="0"
SelectionChanged="LanguageList_SelectionChanged" />
<StackPanel
Margin="25"
HorizontalAlignment="Center"
Orientation="Horizontal">
<!-- 这里绑定字符串的值,Source来源于语言的集合,Mode是响应的方式,有些需要,有些不需要 -->
<TextBlock
Height="40"
Margin="10"
VerticalAlignment="Center"
FontSize="20"
Text="{Binding [Home], Source={x:Static language:LanguageResource.Instance}}" />
<TextBox
Width="150"
Height="40"
Margin="10"
FontSize="20"
Text="{Binding [ToDoList], Source={x:Static language:LanguageResource.Instance}, Mode=OneWay}" />
<Button
Width="150"
Margin="10"
Content="{Binding [OLK], Source={x:Static language:LanguageResource.Instance}}"
FontSize="20" />
</StackPanel>
</WrapPanel>
<Separator
Name="MySeparator"
Grid.Row="1"
Width="Auto"
Height="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Background="Black" />
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ComboBox
x:Name="LanguageComboBox"
Width="125"
VerticalAlignment="Center"
materialDesign:HintAssist.Hint="Language"
FontSize="15"
SelectionChanged="OutlinedComboBox_SelectionChanged"
Style="{StaticResource MaterialDesignOutlinedComboBox}">
<ComboBoxItem Content="zh-CN" />
<ComboBoxItem Content="ja-JP" />
<ComboBoxItem Content="es-EC" />
</ComboBox>
<ListBox
x:Name="MenuBar"
Grid.Column="1"
Margin="0,16,0,16"
AutomationProperties.Name="DemoPagesListBox"
Style="{StaticResource MaterialDesignNavigationPrimaryListBox}">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon
Width="20"
Height="20"
Margin="10,4,0,4"
Kind="Home" />
<TextBlock
Margin="10,5,0,4"
AutomationProperties.AutomationId="DemoItemPage"
FontSize="15"
Text="{Binding [Home], Source={x:Static language:LanguageResource.Instance}}" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon
Width="20"
Height="20"
Margin="10,4,0,4"
Kind="NotebookOutline" />
<TextBlock
Margin="10,5,0,4"
AutomationProperties.AutomationId="DemoItemPage"
FontSize="15"
Text="{Binding [ToDoList], Source={x:Static language:LanguageResource.Instance}}" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon
Width="20"
Height="20"
Margin="10,4,0,4"
Kind="NotebookEditOutline" />
<TextBlock
Margin="10,5,0,4"
AutomationProperties.AutomationId="DemoItemPage"
FontSize="15"
Text="{Binding [OLK], Source={x:Static language:LanguageResource.Instance}}" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon
Width="20"
Height="20"
Margin="10,4,0,4"
Kind="Cog" />
<TextBlock
Margin="10,5,0,4"
AutomationProperties.AutomationId="DemoItemPage"
FontSize="15"
Text="{Binding [Config], Source={x:Static language:LanguageResource.Instance}}" />
</StackPanel>
</ListBoxItem>
</ListBox>
</Grid>
</Grid>
</UserControl>
下面是view裡面的方法
using BlankApp1.Resources.Languages;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BlankApp1.Views.Config
{
/// <summary>
/// ResxView.xaml 的互動邏輯
/// </summary>
public partial class ResxView : UserControl
{
public ResxView()
{
InitializeComponent();
LanguageList.ItemsSource = new List<string>
{
"es-EC",
"zh-CN",
"ja-JP"
};
}
private void LanguageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LanguageResource.Instance.ChangeLanguage(new CultureInfo((sender as ComboBox).SelectedItem.ToString()));
}
private void OutlinedComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectItem = (ComboBoxItem)LanguageComboBox.SelectedItem;
var value = selectItem.Content;
System.Diagnostics.Debug.WriteLine("Combox select item :" + value);
LanguageResource.Instance.ChangeLanguage(new CultureInfo((string)value));
}
}
}
3.創建ViewModel
暫時沒數據處理,是空的,