1:在項(xiàng)目中新建文件夾“Resouce”,然后再該文件夾下面新增資源文件“AppString.resx”,如果創(chuàng)建一個(gè)AppString.resx副本,把文件名改為對(duì)應(yīng)的語(yǔ)言名稱(chēng),
5:接下來(lái)就是語(yǔ)言切換了,我用的是本地存儲(chǔ)的方式來(lái)保存用戶(hù)選擇的語(yǔ)言,新建一個(gè)類(lèi)來(lái)專(zhuān)門(mén)負(fù)責(zé)讀取當(dāng)前用戶(hù)選擇的語(yǔ)言。
public class Configure
{
static System.Globalization.CultureInfo currentCulture;
public static System.Globalization.CultureInfo CurrentCulture
{
get
{
if (currentCulture == null)
{
try
{
System.IO.IsolatedStorage.IsolatedStorageSettings appSetting = 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.IsolatedStorageSettings appSetting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
if (appSetting.Contains("language"))
{
appSetting["language"] = currentCulture.Name;
appSetting.Save();
}
else
{
appSetting.Add("language", currentCulture.Name);
}
}
catch (Exception e)
{
}
}
}
}
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");
}
private void Application_Startup(object sender, StartupEventArgs e)
{
CultureInfo culture = Configure.CurrentCulture;
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
this.RootVisual = new MainPage();
}