在 XAML 中将 RESX 与 Windows Phone 7 结合使用
我目前正在制作一个基本的 Windows Phone 7 应用程序以供娱乐,并且我尝试将字符串(当前仅这些)值存储在 RESX 文件中,而不是直接将它们全部嵌入到 XAML 文件中,甚至添加代码来手动插入值。
在 WPF 中,要使用 RESX 文件中的属性,只需映射包含 RESX: 的命名空间,
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:Namespace.To.Your.Resx;assembly=SuppliedIfSeparate"
然后使用它:
<TextBlock Text="{x:Static resx:Strings.AppTitle}" />
但是,我尝试过,并且我读到 x:Static
只是Silverlight 不支持,并且扩展后,WP7 也不支持 (看来他们已经提供了方法,但没有在 Silverlight 5/Mango 中直接访问 x:Static
(法语)。
<TextBlock Text="{Binding Source={StaticResource AppTitle},Converter={StaticResource ResourceRetriever}}" />
它可以工作,但与 WPF 实现相比,它很冗长且容易出错(在页面甚至控件之间进行大量复制和粘贴)。
对于这种情况,目前的最佳做法是什么?
I am currently making a basic Windows Phone 7 application for fun, and I am trying to store string (currently only those) values in a RESX file rather than embedding them all within the XAML files directly, or even adding code to manually insert the values.
In WPF, to use the property from the RESX file, you simply map the namespace that contains the RESX:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:Namespace.To.Your.Resx;assembly=SuppliedIfSeparate"
and then use it:
<TextBlock Text="{x:Static resx:Strings.AppTitle}" />
But, I tried, and I've read that x:Static
is simply not supported by Silverlight, and by extension, not supported in WP7 (It appears that they have provided the means, but not direct access to x:Static
(French) in Silverlight 5/Mango).
I found this post describing an "AppConverter
" class that is manually created to leverage similar functionality and it is currently what I am doing. The gist of the post is to create a resource within your XAML page that uses the AppConverter class for pre-specified keys, and then using that to replace the x:Static
with a binding.
<TextBlock Text="{Binding Source={StaticResource AppTitle},Converter={StaticResource ResourceRetriever}}" />
It works, but it's verbose and error prone compared to the WPF implementation (a lot of copying and pasting between pages, and even controls).
What is the current best practice for this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MSDN 如何:构建本地化Windows Phone 应用程序
MSDN How to: Build a Localized Application for Windows Phone