WP7完美多语言的实现
实现目标:1.多语言实时切换
2.使用自带的多语言机制
3.支持xaml绑定
实现方法:
step1. 创建语言文件lang.resx
step2. 创建多语言文件lang.resx.zh-cn
step3.右键卸载项目,并编辑。
在 <supportedcultures> zh-cn </supportedcultures> 中添加
step4.创建langhelper辅助类
langhelper.cs
public class langhelper:inotifypropertychanged { public string this[string resourcename] { get { return resource.lang.resourcemanager.getstring(resourcename); } } public event propertychangedeventhandler propertychanged; public void updatebindings() { if (propertychanged != null) propertychanged(this, new propertychangedeventargs("item[]")); } }step5.在mainviewmodel.cs 中添加
///<summary> /// 语言资源访问器(可绑定) ///</summary> public langhelper lang { get; private set; }step6.确定mainpage.xaml.cs中存在datacontext = app.viewmodel;
step7.确定app.xaml.cs中存在
view sourceprint?private static mainviewmodel viewmodel = null;
/// <summary>
/// a static viewmodel used by the views to bind against.
/// </summary>
/// <returns>the mainviewmodel object.</returns>
public static mainviewmodel viewmodel
{
get
{
// delay creation of the view model until necessary
if (viewmodel == null)
viewmodel = new mainviewmodel();
return viewmodel;
}
}
usage:
view sourceprint?{binding lang[appbar_search]}
//其中appbar_search就是你在多语言resx文件中添加的字符串
switch:
view sourceprint?cultureinfo info = new cultureinfo("zh-cn");//en-us;.....
thread.currentthread.currentculture = info;
thread.currentthread.currentuiculture = info;
view sourceprint? app.viewmodel.lang.updatebindings();
作者 一帆风顺可能吗