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();
作者 一帆风顺可能吗
补充:移动开发 , Windows Phone ,