WPF multi-language Solution

Source: Internet
Author: User
SourceCode: Http://files.cnblogs.com/solo/WpfLocalizeTest.zip
Introduction:
MSDN recommends a WPF multi-language solution-using Locbaml is a semi-finished product and not flexible enough. This multilingual solution was modified from: http://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files the solution:
  • Use the WPF extension tag to read the Resx Files content at runtime.
  • Design Mode Design Support
  • Cache with Weak Reference
Due to project requirements, I expanded the code and added the following features:
  • The Provider interface with content can provide multilingual content using Resx File and XML.
  • Error message during design
1. Usage:Use in XAML: Take string and image as an example:
<TextBlock Height="23" HorizontalAlignment="Left" Margin="98,24,0,0" Name="textBlock1"          
Text="{Resx Node={x:Static e:MainWindow.ThemeNode}, Key=lbltxt}"VerticalAlignment="Top"Width="199"/>
 
<Image Height="150" HorizontalAlignment="Left" Margin="98,87,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="251"      
Source="{Resx Node=WpfLocalizeTest.MainWindow, Key=flag}"/>
Use in the Code:
ResManager.Instance.GetResource(nodename, key);
Theme folder structure: en-us is the theme name. WpfLocalizeTest is the Assembly file name. The following Resource. xml is the multilingual Resource file.

Resource. xml:
<?xml version="1.0" encoding="UTF-8"?>
<res>    
     <node name="MainWindow">
<Item key = "lbltxt" type = "string" des = "this is a comment" value = "this is a Chinese interface"/>
<Item key = "flag" type = "image" des = "this is a comment" value = "flag.jpg"/>
     </node>
</res>
 
ResxIs an extension tag, where Node is the first half of WpfLocalizeTest. MainWindow. 
WpfLocalizeTestIs the name of the current dll or exe Assembly file. 
MainWinowIs the xml node name (or no, if not, items are directly stored under the res node .). 
{X: Static e: MainWindow. ThemeNode}For more information, see the code. 
2. supported during design: 
  • Dynamic Switching: In Design mode, there is a earth tray icon that can be used to switch theme and see the effect during Design.

    (There is a problem here: there is a need to write the path at the design stage. Is there a better way to get the file path at the design stage ?)
  • Error Message: If the corresponding key is not configured in xml, the Error window will prompt an Error: (The MainWindow1 node does not exist in the example)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.