Skip to main content.

公告

搜索

最新评论

WP7如何实现多语言支持

实现目标:1.多语言实时切换

     2.使用系统自带的多语言机制

       3.支持xaml绑定

实现方法:

Step1. 创建语言文件lang.resx

Step2. 创建多语言文件lang.resx.zh-cn

Step3.右键卸载项目,并编辑。

      在    <SupportedCultures>       zh-cn     </SupportedCultures> 中添加

Step4.创建langhelper辅助类

    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中存在
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:
{Binding lang[appbar_search]}
//其中appbar_search就是你在多语言resx文件中添加的字符串
Switch:
CultureInfo info = new CultureInfo("zh-cn");//en-us;.....
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = info;
原文链接:http://www.cnblogs.com/sailing/archive/2011/11/13/2247104.html

标签: wp7 多语言

Feedback

你还可以输入600/600个字符 发表评论
称呼: (必填) 登录 | 开通博客
邮箱: (选填) 你的邮箱地址不会被公开
网站: (选填)
验证码: (必填)