如何在Silverlight4項目中實現一個多語言功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1:在項目中新建文件夾“Resouce”,然后再該文件夾下面新增資源文件“AppString.resx”,如果創建一個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();
}
關于如何在Silverlight4項目中實現一個多語言功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。