【发布时间】:2009-09-04 00:57:13
【问题描述】:
我想检测区域设置更改并在 WPF 应用程序中以正确格式显示日期。但是 CultureInfo.ClearCachedData 有一个奇怪的问题。它随机地要么工作要么不工作。有谁知道为什么,以及解决方法?我知道区域设置存储在注册表中,但是破译 HKCU\Control Panel\International 的内容并从中手动构建 CultureInfo 太原始了。
我把它放在一个更大的应用程序中,CultureInfo.ClearCachedData 的失败率几乎是 100%。
Window1.xaml.cs:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Interop;
namespace WpfApplication1
{
partial class Window1 : Window
{
int i = 0;
public Window1()
{
InitializeComponent();
ShownCurrentCulture();
Loaded += (x, y) => HwndSource.FromHwnd(
new WindowInteropHelper(this).Handle).AddHook(WndProc);
}
IntPtr WndProc(IntPtr hwnd, int msg,
IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == 0x1a) // WM_SETTINGCHANGE
{
// CultureInfo.CurrentCulture is sometimes changed,
// sometimes not
ShownCurrentCulture();
}
return IntPtr.Zero;
}
void ShownCurrentCulture()
{
CultureInfo.CurrentCulture.ClearCachedData();
Title = i++ + " " + CultureInfo.CurrentCulture;
}
}
}
【问题讨论】:
标签: c# wpf datetime localization