惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
S
Securelist
小众软件
小众软件
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
O
OpenAI News
Cloudbric
Cloudbric
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
V2EX
PCI Perspectives
PCI Perspectives
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
M
MIT News - Artificial intelligence
V2EX - 技术
V2EX - 技术
阮一峰的网络日志
阮一峰的网络日志
Hacker News - Newest:
Hacker News - Newest: "LLM"
G
Google Developers Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Last Watchdog
The Last Watchdog
The Register - Security
The Register - Security
腾讯CDC
N
News and Events Feed by Topic
C
Check Point Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
S
Schneier on Security
MyScale Blog
MyScale Blog
N
News | PayPal Newsroom
Recorded Future
Recorded Future
T
Tenable Blog
I
InfoQ
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Engineering at Meta
Engineering at Meta

博客园 - Haozes

新Blog 拼写检查算法 Golang 版 Windbg 离线调试.Net 程序入门 (译)你必须知道的位运算技巧 Low Level Bit Hacks You Absolutely Must Know Windows 下 命令行增强工具 WPF Layout & Image异步加载 几篇文章了解编译原理 WPF Binding Validation 数据验证 WPF 实现Loading效果 推荐一个.NET 命令行参数Parser 库 常用开发工具介绍 使用.Net Memory Profiler 分析.Net程序内存泄露 使用Mdbg.exe 调试.Net 程序 .Net 2 Tip :捕获CSE和Thread.Timer与Thread.Sleep比较 使用CSharp Driver操作Mongodb介绍 使用Python操作MSSQL数据库. 运行.Net4.0程序是否要安装之前的.Net版本 javascript Disable <div> or other tag in Other Browser like FF,Chrome Delphi 无类型参数传递动态数组和静态数组
WPF 多语言方案
Haozes · 2012-02-06 · via 博客园 - Haozes

SourceCode:


简介:

MSDN 推荐了一个WPF多语言方案--使用Locbaml ,是个半成品,而且也不够灵活.

此多语方案修改自:

该解决方案:

  • 使用WPF 扩展标记,运行时读取Resx Files内容.
  • Design Mode的设计时支持
  • 使用Weak Reference 缓存

因为项目需求,我拓展了该代码,增加了功能:

  • 加了个内容的Provider接口可使用Resx File和XML等提供多语言内容
  • 设计时错误提示

1.使用方式:

在XAML 中使用:以字符串和 图片为例:

<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}"/>

在代码中使用:

ResManager.Instance.GetResource(nodename, key);

Theme文件夹结构:

en-us 为theme名称. WpfLocalizeTest是程序集文件名,下面Resource.xml为 多语言资源文件

Resource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<res>    
     <node name="MainWindow">
          <item key="lbltxt" type="string" des="这是注释" value="这是中文界面"/>        
           <item key="flag" type="image"  des="这是注释" value="flag.jpg"/>    
     </node>
</res>

Resx 为扩展标记,其中Node为人为约定  :WpfLocalizeTest.MainWindow

前半部分WpfLocalizeTest 是当前dll或exe 程序集文件名称.

后面部分 MainWinow是 xml结点名称(也可以没有,如果没有的话,item直接存放在res结点下.).

2.设计时支持:

  • 动态切换: 在Design 模式时,有个地球托盘图标,使用它可以切换theme,并在Design 时看到效果.

    (此处有个问题:需要写死设计时的路径.有没有更好的方法在设计时获取到文件路径?)

  • 错误提示: 如果在xml没有配置该相应的key.设计时Error窗口会提示报错:(示例中不存在MainWindow1 的node)