【问题标题】:Change culture of Silverlight application改变 Silverlight 应用程序的文化
【发布时间】:2010-07-01 10:19:58
【问题描述】:

我目前正在开发 Silverlight 应用程序。我有一些数据网格/文本块,我使用标准绑定来显示值,其中一些是日期。例如

<sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Path=MyCollection}">
  <sdk:DataGrid.Columns>
    <sdk:DataGridTextColumn Binding="{Binding Path=Name, Mode=OneWay}" Header="Agent"/>
    <sdk:DataGridTextColumn Binding="{Binding Path=UpdateTime, Mode=OneWay}" Header="Update Time"/>
  </sdk:DataGrid.Columns>
</sdk:DataGrid>
<TextBlock Text="{Binding Path=LastUpdatedTime}"/>

这绑定得很好,但日期总是显示为美国风格 (m/d/y) 而我想向他们展示英国风格 (d/m/y)。我尝试使用托管应用程序的页面上的 HTML 标记来设置文化

<param name="uiculture" value="en-GB" />
<param name="culture" value="en-GB" />

在我的 Silverlight 应用程序的 Application_Start 上

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

但这些都没有任何区别。我有一个实现IValueConverter 接口的自定义类,我在 Convert 方法上添加了一个断点,并且传入的 CultureInfo 参数是 en-US,如何更改区域性?

【问题讨论】:

    标签: silverlight localization silverlight-4.0 culture


    【解决方案1】:

    将以下行添加到我的表单的构造函数中解决了这个问题:

    this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
    

    来自http://timheuer.com/blog/archive/2010/08/11/stringformat-and-currentculture-in-silverlight.aspx

    【讨论】:

      【解决方案2】:

      从代码类型中设置值违背了 MVVM 和依赖属性方法:您可能希望看到这篇 SO 文章: How to switch UI Culture of data binding on the fly in Silverlight

      【讨论】:

        【解决方案3】:

        在转换器中使用下面的代码

        CultureInfo ci = new CultureInfo("en-GB");
        return value.ToString(ci);
        

        【讨论】:

        • 我是否需要为每个显示日期的 DataColumn/TextBlock 使用转换器?
        • 是的。但是如果你想使用全局,在 MainPage 构造函数或 Loaded 事件中使用下面(这里不需要转换器) CultureInfo en = new CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = en;
        • 正如我在最初的帖子中所说,我已经尝试在 Application_Start 事件中设置 CurrentCulture,它不会改变日期的显示方式。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-13
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多