Silverlight uses mvvm to implement real-time multi-language switching (including source code)

Source: Internet
Author: User

Because this example is based on mvvmlighttoolkit, our viewmodel base class inherits from the viewmodelbase provided by mvvmlighttoolkit and is named advancedviewmodelbase. All viewmodels in the project inherit from this class. first look at the class diagram:

Because this example is based on mvvmlighttoolkit, our viewmodel base class inherits from the viewmodelbase provided by mvvmlighttoolkit and is named advancedviewmodelbase. All viewmodels in the project inherit from this class.

 

This article focuses on "real-time" switching. We know that when the attributes in viewmodel change, if we want to reflect the changes to the interface, we will use the notification mechanism-inotifypropertychanged. Therefore, we introduce an intermediate "proxy" class observableresources,CodeAs follows:

Public class Observableresources : Inotifypropertychanged { Public String This [ String Resourcename] { Get { Return Language . ResourceManager. getstring (resourcename );}} Public event Propertychangedeventhandler Propertychanged; Public void Updatebindings (){ If (Propertychanged! = Null ) Propertychanged ( This , New Propertychangedeventargs ( "Item []" ));}}

 

 

 

Note: Because the class indexer is used, the parameter of propertychangedeventargs should be "item []", and the language class is automatically generated for the resource file.

Then, we instantiate It In The advancedviewmodelbase class. The key code of advancedviewmodelbase is as follows:

 

        Public  Observableresources  Languageresource {  Get  ;  Set  ;}  Public  Icommand  Changelanguagecommand {  Get  ;  Set  ;}  Public  Advancedviewmodelbase () {languageresource = New  Observableresources  (); Changelanguagecommand =  New  Relaycommand  <  Edslanguage  > (Changelanguage );}  Private void  Changelanguage (  Edslanguage  Lang ){  VaR  Culture =  New Cultureinfo  (Lang. culturename );  Thread  . Currentthread. currentculture = culture;  Thread  . Currentthread. currentuiculture = culture; initialize ageresource. updatebindings ();} 

Here we have prepared an icommand for binding on the interface. When the changelanguagecommand is activated, run the changelanguage method (passing the selected language type ), call the updatebindings method of languageresource to notify the current culture change on the interface.

The edslanguage class is custom and defines the culture, icon, and language information.

In XAML, you need to call

<TextblockGrid. Row ="0"Grid. Column ="0"TEXT = "{BindingLanguageresource [Login_usernamelabel]}"/>

 

Login_usernamelabel is the name in the resource file.

Example of changing the language type (eventtocommand is used in this example, but other methods are also supported ):

 <  ComboBox  Selecteditem = "{ Binding  Selectedlanguage}  "  Itemssource = "{  Binding  Edslanglist}  "  X: Name =  "Cblanguage"  Width =  "110"  Height =  "30"  Horizontalalignment =  "Left"  > <  I :  Interaction. triggers  > <  I  :  Eventtrigger  Eventname =  "Selectionchanged"  > <  CMD  :  Eventtocommand  Command = "{  Binding  Changelanguagecommand}  " Commandparameter = "{  Binding  Elementname =  Cblanguage  , Path =  Selecteditem  }  "  /> </  I  :  Eventtrigger  > </  I  :  Interaction. triggers  > < ComboBox. itemtemplate  > <  Datatemplate  > <  Stackpanel  Orientation =  "Horizontal"  > <  Image  Width =  "28"  Height =  "28"  Source = "{  Binding  Flagicon} "  /> <  Textblock  TEXT = "{  Binding  Languagename}  "  Verticalalignment =  "Center"  /> </  Stackpanel  > </  Datatemplate  > </  ComboBox. itemtemplate  > </ ComboBox  > 

ExampleSource code: Http://files.cnblogs.com/024hi/SwitchLanguageDemo.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.