IValueConverter 在 Windows Phone 7 中获取错误的文化

发布于 2024-11-30 03:27:54 字数 581 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

も让我眼熟你 2024-12-07 03:27:54

不是错误,而是预期的行为。请参阅 MSConnect 上的这篇文章 默认情况下,WPF 绑定使用错误的 CurrentCulture

解决方案是将 PhoneApplicationPageLanguage 属性设置为 CurrentCulture,如下所示:

Language = XmlLanguage.GetLanguage(
    Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);

或者使用 Language 在 XAML 中指定区域性属性,如下所示:

<TextBlock Language="de-DE" Text="..." />

或者在 PhoneApplicationPage 本身上

<phone:PhoneApplicationPage Language="de-DE" ...

但是更好的解决方案是不使用依赖于 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 your PhoneApplicationPage to the CurrentCulture, like this:

Language = XmlLanguage.GetLanguage(
    Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);

Or alternatively specify the culture in XAML, using the Language attribute, like this:

<TextBlock Language="de-DE" Text="..." />

Or on the PhoneApplicationPage it self

<phone:PhoneApplicationPage Language="de-DE" ...

But 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

南城追梦 2024-12-07 03:27:54

您是否尝试过查找 当前文化

可能是 WP7 中的一个错误,没有被传入。

Have you tried looking up the CurrentCulture?

Might be a bug in WP7 where that isn't being passed in.

八巷 2024-12-07 03:27:54

我刚刚遇到这个问题。

我使用以下方法解决了这个问题:

public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
{   
    return string.Format(culture, "{0:N}, value);   
}

使用区域性来转换控制转换,但您还必须确保将值参数保留为对象。更改它的类型会影响 string.Format 与其交互的方式。

I've just had this problem.

I solved it using the following:

public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
{   
    return string.Format(culture, "{0:N}, value);   
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文