Silverlight4 多语言实现的方法

2025-05-27 0 53

1:在项目中新建文件夹“Resouce”,然后再该文件夹下面新增资源文件“AppString.resx”,如果创建一个AppString.resx副本,把文件名改为对应的语言名称,

如AppString.en-US.resx。,并且把AppString.resx的访问修饰符改为Public

Silverlight4 多语言实现的方法

2:打开AppString.resx的cs文件,查看类的访问修饰符是否Public,如果不是,则改为Public。

3:打开App.xmal文件,添加以下代码,目的是用于其它的页面绑定字符内容的资源文件。

Silverlight4 多语言实现的方法

4:然后再其它页面就可以使用这个资源文件了,我这里用了三种语言

Silverlight4 多语言实现的方法

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 多语言实现的方法

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Silverlight4 多语言实现的方法 https://www.kuaiidc.com/69053.html

相关文章

猜你喜欢
发表评论
暂无评论