IValueConverter 实现上的 CultureInfo
当 ValueConverter 用作绑定的一部分时,Convert
函数的参数之一是 System.Globalization.CultureInfo
对象。
谁能告诉我这个文化对象从哪里获取信息?
我有一些代码可以根据该文化格式化日期。当我访问托管在我的计算机上的 silverlight 控件时,它会正确格式化日期(使用 d/MM/yyyy 格式,该格式在我的计算机上设置为短日期格式)。当我访问不同服务器上托管的相同控件(从我的客户端计算机)时,日期格式为 MM/dd/yyyy hh:mm:ss - 这是完全错误的。巧合的是,服务器上的区域设置设置为与我的客户端计算机相同。
这是我的值转换器的代码:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
if (parameter != null && !string.IsNullOrEmpty(parameter.ToString()))
return ((DateTime)value).ToString(parameter.ToString());
else
return ((DateTime)value).ToString(culture.DateTimeFormat.ShortDatePattern);
}
return value;
}
基本上,可以将特定格式指定为转换器参数,但如果不是,则使用区域性对象的短日期模式。
When a ValueConverter is used as part of a binding, one of the parameters to the Convert
function is a System.Globalization.CultureInfo
object.
Can anyone tell me where this culture object gets its info from?
I have some code that formats a date based on that culture. When i access my silverlight control which is hosted on my machine, it formats the date correctly (using the d/MM/yyyy format, which is set as the short date format on my machine). When i access the same control hosted on a different server (from my client machine), the date is being formatted as MM/dd/yyyy hh:mm:ss - which is totally wrong. Coincidentally the regional settings on the server are set to the same as my client machine.
This is the code for my value converter:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DateTime)
{
if (parameter != null && !string.IsNullOrEmpty(parameter.ToString()))
return ((DateTime)value).ToString(parameter.ToString());
else
return ((DateTime)value).ToString(culture.DateTimeFormat.ShortDatePattern);
}
return value;
}
basically, a specific format can be specified as the converter parameter, but if it isn't then the short date pattern of the culture object is used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这确实很有趣。我不希望服务器与它有任何关系,因为在这两种情况下 Silverlight 都完全在客户端上运行。但我会运行 Fiddler 并查看 HTTP 标头中是否有指定区域设置或语言的内容。我不是 HTTP/IIS 方面的专家,所以我不知道这是否典型,但如果服务器指定了区域设置,浏览器可能会使用它作为默认的 CurrentCulture。
但是看看 Reflector,您的问题的答案是它使用 CultureInfo.CurrentUICulture,除非指定了目标元素的 Language 属性,在这种情况下将使用该属性。您还可以在绑定本身上设置 ConverterCulture,这看起来是最高优先级。
Hm that's interesting indeed. I wouldn't expect the server to have anything to do with it since Silverlight is running entirely on the client in both cases. But I would run Fiddler and see if there's anything in the HTTP headers that specifies a locale or language. I am not an expert on HTTP/IIS so I don't know if this is typical or not, but if the server is specifying a locale, the browser may be using that as the default CurrentCulture.
But looking at Reflector, the answer to your question is that it uses CultureInfo.CurrentUICulture unless the Language property of the target element is specified in which case that is used instead. You can also set a ConverterCulture on the binding itself which appears to be the highest priority.
这是一篇简短的文章:
http://en.csharp-online.net/Localization_Like_the_Pros %E2%80%94CurrentCulture_and_CurrentUICulture
Here's a brief article:
http://en.csharp-online.net/Localization_Like_the_Pros%E2%80%94CurrentCulture_and_CurrentUICulture