WinForm software Multi-language switch, personal insight, reduce the software to the language display coupling degree.
1, design drawings (oneself blind painting of hehe)
2, do the small demo, the interface is as follows
3. Below is the Code show section
1) Form1 Code display
1 namespaceWindowsFormsApplication12 {3 Public Partial classForm1:form4 {5 PublicForm1 ()6 {7 InitializeComponent ();8 setlanguage ();9 }Ten One Private voidsetlanguage () A { - This. Lbusername.text =GlobalData.GlobalLanguage.Login_UserName; - This. Lbpwd.text =GlobalData.GlobalLanguage.Login_UserPwd; the This. Btnlogin.text =GlobalData.GlobalLanguage.Login_Login; - } - } -}
2) GlobalData Class code display
1 namespaceWindowsFormsApplication12 {3 Public classGlobalData4 {5 /// <summary>6 ///system Language (Chinese), Chinese (English) ... )7 /// </summary>8 Public Static stringSystemlanguage = system.configuration.configurationmanager.appsettings["Language"];9 Ten Private StaticLanguage globallanguage; One Public StaticLanguage globallanguage A { - Get - { the if(Globallanguage = =NULL) - { -Globallanguage =NewLanguage (); - returnGloballanguage; + } - returnGloballanguage; + } A } at - } -}
3) Language Class code display
1 namespaceWindowsFormsApplication12 {3 Public classLanguage4 {5 #regionLogin interface6 Public stringLogin_username ="";7 Public stringLogin_userpwd ="";8 Public stringLogin_login ="";9 #endregion Ten One protecteddictionary<string,string> diclanguage =Newdictionary<string,string>(); A PublicLanguage () - { - xmlload (globaldata.systemlanguage); the Bindlanguagetext (); - } - - /// <summary> + ///reading XML into memory - /// </summary> + /// <param name= "language" ></param> A protected voidXmlload (stringlanguage) at { - Try - { -XmlDocument doc =NewXmlDocument (); - stringAddress = AppDomain.CurrentDomain.BaseDirectory +"languages\\"+ Language +". XML"; - Doc. Load (address); inXmlElement root =Doc. documentelement; - toXmlNodeList NodeLst1 =Root. ChildNodes; + foreach(XmlNode IteminchnodeLst1) - { the Diclanguage.add (item. Name, item. InnerText); * } $ }Panax Notoginseng Catch(Exception ex) - { the Throwex; + } A } the + Public voidBindlanguagetext () - { $Login_username = diclanguage["Login_username"]; $Login_userpwd = diclanguage["login_userpwd"]; -Login_login = diclanguage["Login_login"]; - } the } -}
4) App. Config code show
1 <?xml version= " 1.0 encoding=" utf-8 ? >2 < Configuration>3 <appsettings>4 <!--The value of language can only be Chinese (Chinese), English-->5 <add Key=
"
language " Value= " english " />6 </appsettings>7 </configuration>
5) English.xml Code display
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 Code display
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Chinese>3 <Login_UserName> user name:</login_username>4 < login_userpwd> Password:</login_userpwd>5 <Login_Login> login </Login_Login>6 </Chinese>
4, if you want to change French, you only need to configure in app. Config for French, and then add a french.xml, content and other language templates to change the content to French.
5, OK.
C # WinForm Software multi-lingual (Chinese, English ... ) interface switching, low coupling