1、首先新建兩個字典文件en-us.xaml、zh-cn.xaml。定義中英文的字符串在這里面。
2、將兩個資源字典添加到App.xaml中,這里注意下,因為兩個字典中有同樣字符,如果沒有動態更改,默認后添加的生效
<ResourceDictionary Source="/Resourcedictionaries\en-us.xaml"/> <ResourceDictionary Source="/Resourcedictionaries\zh-cn.xaml"/>
3、 如何動態切換資源字典
System.Windows.ResourceDictionary resource = new System.Windows.ResourceDictionary();//定義一個資源字典的類型 string requestedCulture = @"/Resourcedictionaries\zh-cn.xaml";設置資源字典的路徑 resource.Source = new Uri(requestedCulture, UriKind.RelativeOrAbsolute);//將路勁存進資源字典 Application.Current.Resources.MergedDictionaries.Remove(resource);刪除相關字典 Application.Current.Resources.MergedDictionaries.Add(resource);添加字典
4:如何調用資源字典的樣式 前端我們一般DynamicResource +key
后台則是FindResource("資源字典的KEY")as string
但是在MVVM 模式下是得不到這個方法的 所以應該用到Application.Current.TryFindResource("資源字典的KEY) as string;這個方法來獲取資源字典的內容