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>