C# Winform软件多语言(汉语、英语。。。)界面的切换,低耦合

Winform软件多语言切换,个人见解,降低软件对语言展示的耦合度。

1、设计图(自己瞎画的呵呵)

2、做的小demo,界面如下

3、下面是代码展示部分

    1)Form1代码展示

 1 namespace WindowsFormsApplication1
 2 {
 3     public partial class Form1 : Form
 4     {
 5         public Form1()
 6         {
 7             InitializeComponent();
 8             SetLanguage();
 9         }
10 
11         private void SetLanguage()
12         {
13             this.lbUserName.Text = GlobalData.GlobalLanguage.Login_UserName;
14             this.lbPwd.Text = GlobalData.GlobalLanguage.Login_UserPwd;
15             this.btnLogin.Text = GlobalData.GlobalLanguage.Login_Login;
16         }
17     }
18 }

    2)GlobalData类代码展示

 1 namespace WindowsFormsApplication1
 2 {
 3     public class GlobalData
 4     {
 5         /// <summary>
 6         /// 系统语言(Chinese(中文),English(英文)。。。)
 7         /// </summary>
 8         public static string SystemLanguage = System.Configuration.ConfigurationManager.AppSettings["Language"];
 9 
10         private static Language globalLanguage;
11         public static Language GlobalLanguage
12         {
13             get
14             {
15                 if (globalLanguage == null)
16                 {
17                     globalLanguage = new Language();
18                     return globalLanguage;
19                 }
20                 return globalLanguage;
21             }
22         }
23         
24     }
25 }

    3)Language类代码展示

 1 namespace WindowsFormsApplication1
 2 {
 3     public class Language
 4     {
 5         #region 登陆界面
 6         public string Login_UserName = "";
 7         public string Login_UserPwd = "";
 8         public string Login_Login = "";
 9         #endregion               
10 
11         protected Dictionary<string, string> DicLanguage = new Dictionary<string, string>();
12         public Language()
13         {
14             XmlLoad(GlobalData.SystemLanguage);
15             BindLanguageText();
16         }
17 
18         /// <summary>
19         /// 读取XML放到内存
20         /// </summary>
21         /// <param name="language"></param>
22         protected void XmlLoad(string language)
23         {
24             try
25             {
26                 XmlDocument doc = new XmlDocument();
27                 string address = AppDomain.CurrentDomain.BaseDirectory + "Languages\\" + language + ".xml";
28                 doc.Load(address);
29                 XmlElement root = doc.DocumentElement;
30 
31                 XmlNodeList nodeLst1 = root.ChildNodes;
32                 foreach (XmlNode item in nodeLst1)
33                 {
34                     DicLanguage.Add(item.Name, item.InnerText);
35                 }
36             }
37             catch (Exception ex)
38             {                
39                 throw ex;
40             }            
41         }
42 
43         public void BindLanguageText()
44         {
45             Login_UserName = DicLanguage["Login_UserName"];
46             Login_UserPwd = DicLanguage["Login_UserPwd"];
47             Login_Login = DicLanguage["Login_Login"];
48         }
49     }
50 }

    4)App.config代码展示

1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <appSettings>
4     <!-- Language的值只能是Chinese(中文),English(英文) -->
5     <add key="Language" value="English"/>
6   </appSettings>
7 </configuration>

    5)English.xml代码展示

1 <?xml version="1.0" encoding="utf-8" ?>
2 <English>
3   <Login_UserName>UserName:</Login_UserName>
4   <Login_UserPwd>Password:</Login_UserPwd>
5   <Login_Login>Login</Login_Login>
6 </English>

    6)Chinese.xml代码展示

1 <?xml version="1.0" encoding="utf-8" ?>
2 <Chinese>
3   <Login_UserName>用户名:</Login_UserName>
4   <Login_UserPwd>密码:</Login_UserPwd>
5   <Login_Login>登录</Login_Login>
6 </Chinese>

4、如果想要换法语则只需要在App.config中配置换成French,然后再添加一个French.xml,内容和其他语言模板一样把内容换成法语则可。

5、OK。

posted @ 2015-12-28 21:31  Materben  阅读(13770)  评论(1编辑  收藏  举报