Silverlight implements real-time multi-language switching through MVVM

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, the Code is as 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 extends ageresource {get; set ;}
Public ICommand ChangeLanguageCommand {get; set ;}

Public AdvancedViewModelBase (){
Repeated ageresource = 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;
LanguageResource. 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

<TextBlock Grid. Row = "0" Grid. Column = "0" Text = "{Binding your ageresource [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 your agename}" VerticalAlignment = "Center"/>
</StackPanel>
</DataTemplate>
</ComboBox. ItemTemplate>
</ComboBox>

Related Article

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.