System.Globalization.CultureInfo.CurrentUICulture 属性

本文提供了此 API 参考文档的补充说明。

CurrentUICulture 属性是每线程属性。 也就是说,每个线程都有自己的当前 UI 区域性。 此属性等效于检索或设置 CultureInfo 分配给该 System.Threading.Thread.CurrentThread.CurrentUICulture 属性的对象。 启动线程时,其 UI 区域性最初确定如下:

  • 通过检索由 DefaultThreadCurrentUICulture 执行线程的应用程序域中的属性指定的区域性(如果属性值不是 null)。

  • 如果线程是执行基于任务的异步操作的线程池线程,并且应用面向 .NET Framework 4.6 或更高版本的 .NET Framework,则其 UI 区域性由调用线程的 UI 区域性确定。 以下示例将当前 UI 区域性更改为葡萄牙语(巴西),并启动六个任务,其中每个任务显示其线程 ID、任务 ID 和当前 UI 区域性。 每个任务(和线程)都继承了调用线程的 UI 区域性。

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Versioning;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example
    {
        public static async Task Main()
        {
            var tasks = new List<Task>();
            Console.WriteLine("The current UI culture is {0}",
                              Thread.CurrentThread.CurrentUICulture.Name);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");
            // Change the current UI culture to Portuguese (Brazil).
            Console.WriteLine("Current UI culture changed to {0}",
                              Thread.CurrentThread.CurrentUICulture.Name);
            Console.WriteLine("Application thread is thread {0}",
                              Thread.CurrentThread.ManagedThreadId);
            // Launch six tasks and display their current culture.
            for (int ctr = 0; ctr <= 5; ctr++)
                tasks.Add(Task.Run(() =>
                {
                    Console.WriteLine("UI Culture of task {0} on thread {1} is {2}",
                                      Task.CurrentId,
                                      Thread.CurrentThread.ManagedThreadId,
                                      Thread.CurrentThread.CurrentUICulture.Name);
                }));
    
            await Task.WhenAll(tasks.ToArray());
        }
    }
    // The example displays output like the following:
    //     The current UI culture is en-US
    //     Current UI culture changed to pt-BR
    //     Application thread is thread 9
    //     UI Culture of task 2 on thread 11 is pt-BR
    //     UI Culture of task 1 on thread 10 is pt-BR
    //     UI Culture of task 3 on thread 11 is pt-BR
    //     UI Culture of task 5 on thread 11 is pt-BR
    //     UI Culture of task 6 on thread 11 is pt-BR
    //     UI Culture of task 4 on thread 10 is pt-BR
    
    Imports System.Globalization
    Imports System.Threading
    
    Module Example1
        Public Sub Main()
            Dim tasks As New List(Of Task)
            Console.WriteLine("The current UI culture is {0}",
                              Thread.CurrentThread.CurrentUICulture.Name)
            Thread.CurrentThread.CurrentUICulture = New CultureInfo("pt-BR")
            ' Change the current UI culture to Portuguese (Brazil).
            Console.WriteLine("Current culture changed to {0}",
                              Thread.CurrentThread.CurrentUICulture.Name)
            Console.WriteLine("Application thread is thread {0}",
                              Thread.CurrentThread.ManagedThreadId)
            ' Launch six tasks and display their current culture.
            For ctr As Integer = 0 To 5
                tasks.Add(Task.Run(Sub()
                                       Console.WriteLine("Culture of task {0} on thread {1} is {2}",
                                                         Task.CurrentId,
                                                         Thread.CurrentThread.ManagedThreadId,
                                                         Thread.CurrentThread.CurrentUICulture.Name)
                                   End Sub))
            Next
            Task.WaitAll(tasks.ToArray())
        End Sub
    End Module
    ' The example displays output like the following:
    '     The current culture is en-US
    '     Current culture changed to pt-BR
    '     Application thread is thread 9
    '     Culture of task 2 on thread 11 is pt-BR
    '     Culture of task 1 on thread 10 is pt-BR
    '     Culture of task 3 on thread 11 is pt-BR
    '     Culture of task 5 on thread 11 is pt-BR
    '     Culture of task 6 on thread 11 is pt-BR
    '     Culture of task 4 on thread 10 is pt-BR
    

    有关详细信息,请参阅文档中的“区域性和基于任务的异步操作”部分 CultureInfo

  • 通过调用 Windows GetUserDefaultUILanguage 函数。

若要更改线程使用的用户界面区域性,请将属性 Thread.CurrentUICulture 设置为新区域性。 如果以这种方式显式更改线程的 UI 区域性,则当线程跨越应用程序域边界时,该更改将保持不变。

注意

如果将属性值设置为 CultureInfo 表示新区域性的对象,该属性的值 Thread.CurrentThread.CurrentCulture 也会更改。

获取当前的 UI 区域性

CultureInfo.CurrentUICulture 属性是每线程设置;也就是说,每个线程可以有自己的 UI 区域性。 通过检索属性的值来获取当前线程的 CultureInfo.CurrentUICulture UI 区域性,如以下示例所示。

using System;
using System.Globalization;

public class Example2
{
    public static void Main()
    {
        CultureInfo culture = CultureInfo.CurrentUICulture;
        Console.WriteLine("The current UI culture is {0} [{1}]",
                          culture.NativeName, culture.Name);
    }
}
// The example displays output like the following:
//       The current UI culture is English (United States) [en-US]
Imports System.Globalization

Module Example3
    Public Sub Main()
        Dim culture As CultureInfo = CultureInfo.CurrentCulture
        Console.WriteLine("The current UI culture is {0} [{1}]",
                        culture.NativeName, culture.Name)
    End Sub
End Module
' The example displays output like the following:
'     The current UI culture is English (United States) [en-US]

还可以从 Thread.CurrentUICulture 属性中检索当前线程的 UI 区域性的值。

显式设置当前 UI 区域性

从 .NET Framework 4.6 开始,可以通过向属性分配 CultureInfo 表示新区域性的对象来更改当前 UI 区域性 CultureInfo.CurrentUICulture 。 当前 UI 区域性可以设置为特定区域性(例如 en-US 或 de-DE)或中性区域性(例如 en 或 de)。 以下示例将当前 UI 区域性设置为 fr-FR 或法语(法国)。

using System;
using System.Globalization;

public class Example1
{
    public static void Main()
    {
        Console.WriteLine("The current UI culture: {0}",
                          CultureInfo.CurrentUICulture.Name);

        CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
        Console.WriteLine("The current UI culture: {0}",
                          CultureInfo.CurrentUICulture.Name);
    }
}
// The example displays output like the following:
//       The current UI culture: en-US
//       The current UI culture: fr-FR
Imports System.Globalization

Module Example2
    Public Sub Main()
        Console.WriteLine("The current UI culture: {0}",
                        CultureInfo.CurrentUICulture.Name)

        CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR")
        Console.WriteLine("The current UI culture: {0}",
                        CultureInfo.CurrentUICulture.Name)
    End Sub
End Module
' The example displays output like the following:
'       The current UI culture: en-US
'       The current UI culture: fr-FR

在多线程应用程序中,可以通过向线程的属性分配 CultureInfo 表示该区域性的对象来显式设置任何线程的 Thread.CurrentUICulture UI 区域性。 如果要设置其区域性的线程是当前线程,可以将新区域性分配给 CultureInfo.CurrentUICulture 该属性。 显式设置线程的 UI 区域性时,即使线程跨越应用程序域边界并在另一个应用程序域中执行代码,该线程也会保留相同的区域性。

隐式设置当前 UI 区域性

首次创建线程(包括主应用程序线程)时,默认情况下,其当前 UI 区域性设置为如下:

  • 如果属性值不是null,则使用当前应用程序域的属性定义的DefaultThreadCurrentUICulture区域性。
  • 通过使用系统的默认区域性。 在使用 Windows 操作系统的系统上,公共语言运行时调用 Windows GetUserDefaultUILanguage 函数来设置当前的 UI 区域性。 GetUserDefaultUILanguage 返回用户设置的默认 UI 区域性。 如果用户尚未设置默认 UI 语言,它将返回最初安装在系统上的区域性。

如果线程跨越应用程序边界并在另一个应用程序域中执行代码,则其区域性与新创建的线程的区域性相同。

请注意,如果设置与系统安装的 UI 区域性或用户的首选 UI 区域性不同的特定 UI 区域性,并且应用程序启动多个线程,则这些线程的当前 UI 区域性将是函数返回 GetUserDefaultUILanguage 的区域性 DefaultThreadCurrentUICulture ,除非为线程正在执行的应用程序域中的属性分配区域性。

安全注意事项

更改当前线程的区域性需要具有 SecurityPermission 设置值的权限 ControlThread

注意

由于与线程关联的安全状态,操作线程是危险的。 因此,此权限应仅提供给可信代码,然后仅在必要时提供。 不能在半受信任的代码中更改线程区域性。

当前 UI 区域性和 UWP 应用

在通用 Windows 平台(UWP)应用中,该属性是读写的CurrentUICulture,就像在 .NET Framework 和 .NET Core 应用中一样;你可以使用它来获取和设置当前区域性。 但是,UWP 应用不区分当前区域性和当前 UI 区域性。 CurrentCultureCurrentUICulture 属性映射到 Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages 集合中的第一个值。

在 .NET Framework 和 .NET Core 应用中,当前 UI 区域性是每线程设置,并且 CurrentUICulture 该属性仅反映当前线程的 UI 区域性。 在 UWP 应用中,当前区域性映射到 Windows.ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages 属性,该属性是全局设置。 CurrentCulture设置属性会更改整个应用的区域性;区域性不能按线程设置。