WPF internationalized way of a resource file

Look at the effect of it, personally feel special mechanism properties change due to UI MVVM pattern of response, resource files and data memory are international good choice.

 

 

 

 

 

1. First prepare two resource files used to make the use of Chinese and English conversion, you will need to convert language programs are inflicted two versions into two resource file.

 

 The one as the default language, will generate operating resource file default language is changed to "Page" .

 

 

 

 

 

 

 The properties of other language files changed to the following:

 

 

 2. The translation strings need to use the procedure for each version, placed into the resource file corresponding.

Chinese.xaml

 

 

 English.xaml

 

 

 

 

 

 3 Import default language among App.xaml resource file as a global resource, if you do not want to use a global, but a specific form is added into the Resources of the corresponding form.

4.

(1) using the data dictionary resources in xaml:

Directly attribute value = "  {} the DynamicResource the ResourceKey = XXX" Key Value resource dictionary XXX

 

 

 

 

(2) use of resources cs dictionary in the background data in the file:

FindResource ( "XXX"); key values ​​as resources XXX

The premise of the form can obtain this resource

(3) data dictionary in the use of resources viewModel file

Will form as a parameter passed to ViewModel by construction, otherwise known yet. . .

Using MVVM pattern, you can try to write resources to get the other two dictionary objects, you can modify the object at the time of handover, notification of property due to the mechanism automatically updated UI.

 

5. Switch resource dictionary:

Remember to determine whether there has been an international language dictionary resources of resource set, if there is first out of the time of handover, adding to switch language resources, or will conflict.

If you add a resource dictionary in App.xaml, steps

(1) access to resources dictionary

 

 (2)移除已有再添加,此处判断方法是判断App.xaml的所有的资源字典中是否存在包含某个键值的资源字典,如果存在就表明他是语言的资源文件,因为还有其他的需要用到的不能都移除,只需移除语言的资源

 

 如果资源字典添加到窗体的Resource之中的,只需将上面图中的"Application.Current.Resources.MergedDictionaries"改为" {你的window对象}.Resources.MergedDictionaries"其他一致。

Guess you like

Origin www.cnblogs.com/qwqwQAQ/p/11595454.html