IValueConverter 在 Windows Phone 7 中获取错误的文化
我在 Windows Phone 7 中创建了一个值转换器……
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// ...
}
// ...
}
并像这样使用它……
<TextBlock Text="{Binding SomeField, Converter={StaticResource MyConverter}, ConverterParameter=SomeParameter}" <!-- ... --> />
我的问题:Convert 方法的 argument
区域性始终为“en-US”,甚至当我将 Windows Phone 设备(或模拟器)的文化更改为德国时,culture
参数仍保持英语。
I created a value converter in my Windows Phone 7 ...
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// ...
}
// ...
}
... and use it like this ...
<TextBlock Text="{Binding SomeField, Converter={StaticResource MyConverter}, ConverterParameter=SomeParameter}" <!-- ... --> />
My problem: The argument
culture of the Convert method is always "en-US", even when I change the culture of the Windows Phone device (or emulator) say to german Germany, the culture
argument stays english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是错误,而是预期的行为。请参阅 MSConnect 上的这篇文章 默认情况下,WPF 绑定使用错误的 CurrentCulture。
解决方案是将
PhoneApplicationPage
的Language
属性设置为 CurrentCulture,如下所示:或者使用
Language
在 XAML 中指定区域性属性,如下所示:或者在
PhoneApplicationPage
本身上但是更好的解决方案是不使用依赖于
culture
参数的值转换器。编辑:我在博客中介绍了替代解决方案:DateTime 格式化一个值转换器
Not a bug, intended behaviour. See this post on MSConnect WPF Binding uses the wrong CurrentCulture by default.
The solution is to set the
Language
property of yourPhoneApplicationPage
to the CurrentCulture, like this:Or alternatively specify the culture in XAML, using the
Language
attribute, like this:Or on the
PhoneApplicationPage
it selfBut a much better solution is not to have a value-converter that's depending on the
culture
argument.Edit: I blogged about a alternative solution: DateTime formatting in a ValueConverter
您是否尝试过查找 当前文化?
可能是 WP7 中的一个错误,没有被传入。
Have you tried looking up the CurrentCulture?
Might be a bug in WP7 where that isn't being passed in.
我刚刚遇到这个问题。
我使用以下方法解决了这个问题:
使用区域性来转换控制转换,但您还必须确保将值参数保留为对象。更改它的类型会影响 string.Format 与其交互的方式。
I've just had this problem.
I solved it using the following:
Use culture to convert control the conversion, but you must also make sure you leave the value parameter as an object. Changing it's type affects how the string.Format interacts with it.