1: create a new folder "Resouce" in the project, and then add the resource file "AppString. resx ", if you create an AppString. resx copy, change the file name to the corresponding language name,
Such as AppString. en-US.resx ., Change the access modifier of AppString. resx to Public.
2: Open the cs file of AppString. resx and check whether the class access modifier is Public. If not, change it to Public.
3: Open the App. xmal file and add the following code to bind the resource file with characters on other pages.
4: You can use this resource file on other pages. I use three languages here.
5: the language is switched. I use local storage to save the language selected by the user, and create a class to read the language selected by the user.
Copy codeThe Code is as follows:
Public class Configure
{
Static System. Globalization. CultureInfo currentCulture;
Public static System. Globalization. CultureInfo CurrentCulture
{
Get
{
If (currentCulture = null)
{
Try
{
System. IO. IsolatedStorage. isolatedstoragesetemedietting = System. IO. IsolatedStorage. IsolatedStorageSettings. ApplicationSettings;
If (appSetting. Contains ("language "))
{
CurrentCulture = new System. Globalization. CultureInfo (string) appSetting ["language"]);
}
}
Catch (Exception e)
{
}
}
If (currentCulture = null)
{
CurrentCulture = new System. Globalization. CultureInfo ("en-us ");
}
Return currentCulture;
}
Set
{
CurrentCulture = value;
System. Threading. Thread. CurrentThread. CurrentCulture = currentCulture;
System. Threading. Thread. CurrentThread. CurrentUICulture = currentCulture;
Try
{
System. IO. IsolatedStorage. isolatedstoragesetemedietting = System. IO. IsolatedStorage. IsolatedStorageSettings. ApplicationSettings;
If (appSetting. Contains ("language "))
{
AppSetting ["language"] = currentCulture. Name;
Deleetting. Save ();
}
Else
{
Deleetting. Add ("language", currentCulture. Name );
}
}
Catch (Exception e)
{
}
}
}
}
The code for the "Switch" button
Copy codeThe Code is as follows:
Private void button3_Click (object sender, RoutedEventArgs e)
{
Configure. CurrentCulture = new CultureInfo (comboBox1.SelectionBoxItem. ToString ());
// If (Configure. CurrentCulture. Name = "zh-CN ")
//{
// Configure. CurrentCulture = new CultureInfo ("en-US ");
//}
// Else
// Configure. CurrentCulture = new CultureInfo ("zh-CN ");
}
6: Finally, the Code started by the application, that is, read the language saved by the user. In the App. xmal. cs file,
Copy codeThe Code is as follows:
Private void Application_Startup (object sender, StartupEventArgs e)
{
CultureInfo culture = Configure. CurrentCulture;
Thread. CurrentThread. CurrentUICulture = culture;
Thread. CurrentThread. CurrentCulture = culture;
This. RootVisual = new MainPage ();
}
Note: After you press the switch button, you must log on to the application again to see the effect, not even if you switch.