Silverlight4 多語言實(shí)現(xiàn)的方法
1:在項(xiàng)目中新建文件夾“Resouce”,然后再該文件夾下面新增資源文件“AppString.resx”,如果創(chuàng)建一個(gè)AppString.resx副本,把文件名改為對(duì)應(yīng)的語言名稱,
如AppString.en-US.resx。,并且把AppString.resx的訪問修飾符改為Public

2:打開AppString.resx的cs文件,查看類的訪問修飾符是否Public,如果不是,則改為Public。
3:打開App.xmal文件,添加以下代碼,目的是用于其它的頁面綁定字符內(nèi)容的資源文件。
4:然后再其它頁面就可以使用這個(gè)資源文件了,我這里用了三種語言
5:接下來就是語言切換了,我用的是本地存儲(chǔ)的方式來保存用戶選擇的語言,新建一個(gè)類來專門負(fù)責(zé)讀取當(dāng)前用戶選擇的語言。
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");
}
6:最后是應(yīng)用程序啟動(dòng)的代碼,也就是讀取用戶保存的語言。在App.xmal.cs文件里,
private void Application_Startup(object sender, StartupEventArgs e)
{
CultureInfo culture = Configure.CurrentCulture;
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
this.RootVisual = new MainPage();
}
注意:按下切換按鈕后要重新登錄應(yīng)用程序才能看到效果,并不是即使切換。
相關(guān)文章
Visual Studio 2017 ASP.NET Core開發(fā)
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017 ASP.NET Core開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03asp.net兩級(jí)聯(lián)動(dòng)(包含添加和修改)
兩級(jí)聯(lián)動(dòng)實(shí)現(xiàn)代碼2009-01-01.NET Core類庫(kù)System.Reflection.DispatchProxy實(shí)現(xiàn)簡(jiǎn)易Aop的方法
這篇文章主要給大家介紹了關(guān)于.NET Core類庫(kù)System.Reflection.DispatchProxy實(shí)現(xiàn)簡(jiǎn)易Aop的相關(guān)資料,文中通過示例代碼結(jié)束的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12C#中OpenFileDialog和PictrueBox的用法分析
這篇文章主要介紹了C#中OpenFileDialog和PictrueBox的用法,以實(shí)例的形式較為詳細(xì)的分析了OpenFileDialog和PictrueBox使用時(shí)的注意事項(xiàng)與具體用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁編碼
HttpWebRequest獲取網(wǎng)頁源代碼時(shí)自動(dòng)識(shí)別網(wǎng)頁編碼,通過讀取頁面中的charset和讀取http頭中的編碼信息獲取頁面的編碼,基本可以正確獲取網(wǎng)頁編碼2008-09-09