当前位置: 移动技术网 > IT编程>移动开发>WP > WP7完美多语言的实现

WP7完美多语言的实现

2018年10月13日  | 移动技术网IT编程  | 我要评论

液晶电视挂墙,舞动乾坤全文阅读,落叶租号网

 

 实现目标: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();

 

  

作者 一帆风顺可能吗

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网