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

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