WPF实现利用资源词典来进行多语言的切换,并通过利用文件读写的方式记忆WPF

参考1:WPF实现无刷新动态切换多种语言
参考2:WPF多国语系实作系列之叁 - 使用ResourceDictionary
StreamReader Class
StreamWriter Class
FileInfo Class
控件准备
ComboBox;又称为下拉组合框控件,他主要用于下拉组合框中显示数据。
当下拉列表的选项发生改变时,将会引发控件SelectedValueChanged事件。
comboBox2.Items.Add("English");//下拉列表加入元素 comboBox2.Items.Add("中文");
文件基本操作
FileInfo类

///FileInfo finfo = new FileInfo("C:\\Test.txt");创建文件对象
//我在工程中这么使用创建一个文本
FileInfo fi1 = new FileInfo("text1");//直接在debug中建立文件text1
//进行文件的读写
 using (StreamWriter sw = fi1.CreateText()) //Create a file to write to.
  {
     sw.WriteLine(0);//文本中有0和/r/n
     // sw.Write(0);文本中只有0
   }
StreamReader sr = new StreamReader("text1");
 textBox.Text = sr.ReadToEnd();
sr.Close();//关闭当前文件流读取,必须要写,不然他会一直读取

StreamWriter类是专门用来处理文本文件的类,可以方便的向文本文件中写入字符串,同时它也是负责重要的转换以及处理向FileStream对象写入的工作。
方法
close:关闭当前的sringwriter和基础流。
write:写入到stringwriter的此实例中。
writeLine:写入重载参数指定的某些数据,后跟行结束符。这是跟write的主要区别。
StreamWriter类是专门用来读取文本文件的类,它提供了许多用于读取和浏览字符数据的方法。
方法
Close:关闭stringread;
Read:读取输入字符串的下一个字符或者下一组字符
ReadBlock:从当前流中读取最大的count的字符并从index开始将该数据写入Buffer
ReadLine:从基础字符串中读取一行
ReadToEnd:将整个流或从当前位置到流的结尾作为字符串读取。
//在程序开发的过程中,将输入或输出设备之间的数据传递抽象为流。分为字符流和字节流,根据流的流向分为输入流(数据流入到内存中)和输出流。
重点:如何实现多语言切换
首先建立一个简单的WPF专案.
在这里插入图片描述
1.在管理器里建立一个名叫Lang的文件夹,用来存放ResourceDictionary。
在这里插入图片描述
2.分别建立名en_US.xaml,zh_CN.xaml的资源文件。
在这里插入图片描述
在这里插入图片描述
3.编辑.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="OK">确定</sys:String> <sys:String x:Key="Cancel">取消</sys:String> </ResourceDictionary>
默认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="OK">确定</sys:String>
    <sys:String x:Key="Cancel">取消</sys:String>
</ResourceDictionary>

非默认zh_CN.xaml的属性设置
在这里插入图片描述
4.在APP.xaml中配置默认语言:

<Application x:Class="WpGlobalization.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <!--这个节点就是配置默认语言的-->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="lang\en_US.xaml"/>
                <!-- 相对路劲 -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

5.实际使用
界面效果:
在这里插入图片描述
XAML:

<Window x:Class="WpGlobalization.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="cbLang" HorizontalAlignment="Left" Margin="22,19,0,0" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" HorizontalAlignment="Left" Margin="169,19,0,0" VerticalAlignment="Top" Width="75"  Loaded=" cbLang_Loaded" />
        <Button  Content="{DynamicResource OK}" HorizontalAlignment="Left" Margin="79,191,0,0" VerticalAlignment="Top" Width="75"/>
        <Button  Content="{DynamicResource Cancel}" HorizontalAlignment="Left" Margin="235,191,0,0" VerticalAlignment="Top" Width="75"/>
        <ComboBox x:Name="comboBox2" HorizontalAlignment="Left" Margin="79,111,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox2_SelectionChanged"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="309,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="BUTTON1" Content="english" HorizontalAlignment="Left" Margin="340,102,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.IO;
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 WpGlobalization
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        
       
        //控件载入时,为ComboBox赋值//就是窗口加载事件
        private void cbLang_Loaded(object sender, RoutedEventArgs e)
        {          
            StreamReader sr = new StreamReader("text1");
            string a = sr.ReadToEnd();
            sr.Close();
            if (a == "0")
            {
                BUTTON1.Content = "english";
                comboBox2.SelectedIndex = 0;
            }
            if (a == "1")
            {
                BUTTON1.Content = "中文";
                comboBox2.SelectedIndex = 1;
            }

            //string[] str = new string[] { "English", "中文" };
            comboBox2.Items.Add("English");
            comboBox2.Items.Add("中文");
        }
         
        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ResourceDictionary langRd = null;
            if (comboBox2.SelectedIndex == 0)
            {
                try
                {
                    //根据名字载入语言文件//相对路径
                    langRd = Application.LoadComponent(new Uri(@"lang\" + "en_US" + ".xaml", UriKind.Relative)) as ResourceDictionary;
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message);
                }
                if (langRd != null)
                {
                   //如果已使用其他语言,先清空
                    if (this.Resources.MergedDictionaries.Count > 0)
                    {
                        this.Resources.MergedDictionaries.Clear();
                    }
                    this.Resources.MergedDictionaries.Add(langRd);
                }

                FileInfo fi1 = new FileInfo("text1");//直接在debug中建立文件text1
                using (StreamWriter sw = fi1.CreateText()) //Create a file to write to.
                {
                    sw.Write(0);
                }

                StreamReader sr = new StreamReader("text1");
               textBox.Text = sr.ReadToEnd();
               sr.Close();//关闭当前文件流读取,必须要写,不然他会一直读取
            }

            if (comboBox2.SelectedIndex == 1)
            {
                try
                {
                    //根据名字载入语言文件//相对路径
                    langRd = Application.LoadComponent(new Uri(@"lang\" + "zh_CN" + ".xaml", UriKind.Relative)) as ResourceDictionary;//  as ResourceDictionary 强制转换为 ResourceDictionary
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message);
                }
                if (langRd != null)
                {
                    //如果已使用其他语言,先清空
                    if (this.Resources.MergedDictionaries.Count > 0)
                    {
                        this.Resources.MergedDictionaries.Clear();
                    }
                    this.Resources.MergedDictionaries.Add(langRd);
                }
                FileInfo fi1 = new FileInfo("text1");//直接在debug中建立文件text1
                using (StreamWriter sw = fi1.CreateText()) //Create a file to write to.
                {
                    sw.Write(1);
                }

                StreamReader sr = new StreamReader("text1");
                textBox.Text = sr.ReadToEnd();
                sr.Close();
            }
        }
    }
 }
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值