.NET 文化变革事件?

标签 .net culture

我有一个 .NET 自定义控件,它以文化相关的方式显示信息。为了提高性能,我在控件中缓存了一些信息,而不是每次都生成它。我需要知道文化是否发生了变化,以便我可以适本地重新生成这些内部信息。

我有什么事件吗?还是我每次绘画时都必须测试文化设置以查看它是否发生了变化?

最佳答案

您可以处理 SystemEvents.UserPreferenceChanged 事件:

SystemEvents.UserPreferenceChanged += (sender, e) => 
{
    // Regional settings have changed
    if (e.Category == UserPreferenceCategory.Locale)
    {
        // .NET also caches culture settings, so clear them
        CultureInfo.CurrentCulture.ClearCachedData();

        // do some other stuff
    }
};

Windows 还广播 WM_SETTINGSCHANGE message到所有表格。您可以尝试通过覆盖 WndProc 来检测它,所以它看起来像:
// inside a winforms Form
protected override void WndProc(ref Message m)
{
    const int WM_SETTINGCHANGE = 0x001A;

    if (m.Msg == WM_SETTINGCHANGE)
    {
        // .NET also caches culture settings, so clear them
        CultureInfo.CurrentCulture.ClearCachedData();

        // do some other stuff
    }

    base.WndProc(ref m);
}

请注意,您可能还想调用 CultureInfo.CurrentCulture.ClearCachedData() 使新创建线程的文化信息无效,但请记住 CurrentCulture如 MSDN 所述,现有线程将不会更新:

The ClearCachedData method does not refresh the information in the Thread.CurrentCulture property for existing threads. However, future threads will have new CultureInfo property values.

关于.NET 文化变革事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/652505/

相关文章:

c# - 从奇怪的格式解析 c# 中的 DateTime

以独立于文化的方式进行 Powershell 格式化?!对于数字

.net - 文化不可变对象(immutable对象) ToString()

python - 收集 IronPython 并行循环结果

c# - 在 C# SOAP 中开始使用 Paypal 自适应支付

c# - 生成的代码 (xsd.exe) 的 SuppressMessage 编译器警告 CS1591

c# - 寻找具有相同语言的文化

.net - 在进入编辑模式时捕获第一个字符 - DataGridView 中的自定义编辑控件

c# - 如何在 C#/IL 中改变盒装值类型(原始或结构)

c# - 比较法语字符 ¦ 的问题