WPF XAML StringFormat:文化解决方法在C#4.0? [英] WPF XAML StringFormat: Culture Workaround broken in C# 4.0?

查看:181
本文介绍了WPF XAML StringFormat:文化解决方法在C#4.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

...用于工作到现在(这里提到:http://stackoverflow.com/questions/520115/stringfomat-localization-problem/520334#520334 )。



相反,直到我将我的应用程序从3.5SP1移植到4.0,它是工作。但现在在4.0它停止工作了。遇到这种情况的人?



编辑:现在甚至不能在3.5SP1中工作。我认为这与安装4.0有关,因为以前这是工作。



它不工作,通过添加解决方法或删除它。我甚至尝试添加...

  CultureInfo.CurrentCulture.ClearCachedData(); 
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

窗口构造函数。这也没有奏效。

解决方案

1。确保您覆盖默认值 LanguageProperty 。应用程序的静态构造函数是最好的选择。这很重要,因为 BindingExpression 缓存此属性的值,并且由于性能原因不会重新评估。



2。 CultureInfo.CurrentCulture 是什么?

3。覆盖语言您是否确定这是您期望看到的? xml:lang 属性在树的上部。例如。如果你说:

 < StackPanel xml:lang =it> 
< TextBlock Text ={Binding StringFormat = C}/>
< / StackPanel>

无论您在属性元数据中设置了什么,您都将获得意大利货币。



4。如果您指定 ConverterCulture ,覆盖语言 code>属性绑定。例如。如果你说:< TextBlock Text ={Binding StringFormat = C,ConverterCulture = ja}/> 属性元数据或 xml:lang 属性。



据我所知,这种行为在框架之间没有改变。



希望这有助于


The work around...

FrameworkElement.LanguageProperty.OverrideMetadata(
 typeof(FrameworkElement), 
 new FrameworkPropertyMetadata(
  XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

...used to work till now (mentioned here: http://stackoverflow.com/questions/520115/stringfomat-localization-problem/520334#520334).

Instead till I ported my application from 3.5SP1 to 4.0, it was working. But now in 4.0 it stopped working again. Anybody experiencing this?

EDIT: It is now not even working in 3.5SP1. I think this has something to do with the installation of 4.0 as previously this was working.

It is not working by either adding the workaround or removing it. I even tried adding...

CultureInfo.CurrentCulture.ClearCachedData();
this.Language = XmlLanguage.GetLanguage( CultureInfo.CurrentCulture.IetfLanguageTag);

to Window constructor. This also didn't worked.

解决方案

1. Make sure you are overriding default value of LanguageProperty as early as possible. App's static constructor is the best bet. This is important because BindingExpression caches value of this property and does not reevaluate it afterwards for performance reasons.

2. What is your CultureInfo.CurrentCulture? Are you sure it's the one you expect to see?

3. Overriding Language property metadata has no effect if you you specify xml:lang attribute somewhere upper in the tree. E.g. if you say:

<StackPanel xml:lang="it">
  <TextBlock Text="{Binding StringFormat=C}"/>
</StackPanel>

You'll get Italian currency no matter what you set in property metadata.

4. Overriding Language property metadata has no effect if you specify ConverterCulture property in binding. E.g. if you say: <TextBlock Text="{Binding StringFormat=C, ConverterCulture=ja}"/> you'll get Japanese currency no matter what you set either in property metadata or in xml:lang attribute.

This behavior was not changed between frameworks as far as I know.

Hope this helps

这篇关于WPF XAML StringFormat:文化解决方法在C#4.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆