CultureInfo.CurrentUICulture 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 CultureInfo 对象,该对象表示资源管理器在运行时查找区域性特定资源时所用的当前用户接口区域性。
public:
static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public:
static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentUICulture { get; set; }
public static System.Globalization.CultureInfo CurrentUICulture { get; }
member this.CurrentUICulture : System.Globalization.CultureInfo with get, set
member this.CurrentUICulture : System.Globalization.CultureInfo
Public Shared Property CurrentUICulture As CultureInfo
Public Shared ReadOnly Property CurrentUICulture As CultureInfo
属性值
资源管理器用于在运行时查找查找区域性特定资源的区域性。
例外
属性设置为 null
。
该属性设置为不能用于定位资源文件的区域性名称。 资源文件名必须仅包含字母、数字、连字符或下划线。
示例
下面的代码示例演示如何更改当前 CurrentCulture 线程的 和 CurrentUICulture 。
using namespace System;
using namespace System::Globalization;
using namespace System::Threading;
int main()
{
// Display the name of the current thread culture.
Console::WriteLine("CurrentCulture is {0}.", CultureInfo::CurrentCulture->Name);
// Change the current culture to th-TH.
CultureInfo::CurrentCulture = gcnew CultureInfo("th-TH",false);
Console::WriteLine("CurrentCulture is now {0}.", CultureInfo::CurrentCulture->Name);
// Displays the name of the CurrentUICulture of the current thread.
Console::WriteLine("CurrentUICulture is {0}.", CultureInfo::CurrentCulture->Name);
// Changes the CurrentUICulture of the current thread to ja-JP.
CultureInfo::CurrentUICulture = gcnew CultureInfo("ja-JP",false);
Console::WriteLine("CurrentUICulture is now {0}.", CultureInfo::CurrentCulture->Name);
}
// The example displays the following output:
// CurrentCulture is en-US.
// CurrentCulture is now th-TH.
// CurrentUICulture is en-US.
// CurrentUICulture is now ja-JP.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
// Display the name of the current culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);
// Change the current culture to th-TH.
CultureInfo.CurrentCulture = new CultureInfo("th-TH", false);
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);
// Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);
// Change the current UI culture to ja-JP.
CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false );
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
}
// The example displays the following output:
// CurrentCulture is en-US.
// CurrentCulture is now th-TH.
// CurrentUICulture is en-US.
// CurrentUICulture is now ja-JP.
Imports System.Globalization
Imports System.Threading
Public Module Example
Public Sub Main()
' Display the name of the current culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name)
' Change the current culture to th-TH.
CultureInfo.CurrentCulture = New CultureInfo("th-TH", False)
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name)
' Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name)
' Change the current UI culture to ja-JP.
CultureInfo.CurrentUICulture = New CultureInfo("ja-JP", False)
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name)
End Sub
End Module
' The example displays the following output:
' CurrentCulture is en-US.
' CurrentCulture is now th-TH.
' CurrentUICulture is en-US.
' CurrentUICulture is now ja-JP.
注解
当前 UI 区域性为每个线程的属性。 也就是说,每个线程都有自己的当前 UI 区域性。 此属性等效于检索或,从 .NET Framework 4.6 开始,设置 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; [assembly:TargetFramework(".NETFramework,Version=v4.6")] 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.Collections.Generic Imports System.Globalization Imports System.Runtime.Versioning Imports System.Threading Imports System.Threading.Tasks <assembly:TargetFramework(".NETFramework,Version=v4.6")> Module Example 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
函数。
备注
在 .NET Compact Framework 中, CurrentUICulture 属性是只读的。 当前 UI 区域性由系统的区域设置确定,不能以编程方式进行更改。
从 .NET Framework 4.6 开始,若要更改线程使用的用户界面区域性,请将属性设置 Thread.CurrentUICulture 为新的区域性。 如果以这种方式显式更改线程的 UI 区域性,则当线程跨越应用程序域边界时,此更改将保持不变。
备注
在 .NET Framework 4.5.2 及更早版本中, CurrentUICulture 属性是只读的; 也就是说,您可以检索属性值,但不能对其进行设置。 若要更改当前 UI 区域性,请将 CultureInfo 表示新 ui 区域性的对象分配给 Thread.CurrentThread.CurrentUICulture
属性。 从 .NET Framework 4.6 开始,该 CultureInfo.CurrentUICulture 属性是可读写的; 你可以设置和检索属性的值。 如果将属性值设置为 CultureInfo 表示新区域性的对象,则该属性的值 Thread.CurrentThread.CurrentCulture
也会发生更改。
本节内容:
获取当前 UI 区域性
显式设置当前 UI 区域性
隐式设置当前 UI 区域性
安全注意事项
当前 UI 区域性和 Windows 应用\
获取当前 UI 区域性
CultureInfo.CurrentUICulture属性是每个线程的设置; 也就是说,每个线程都可以有自己的 UI 区域性。 可以通过检索属性的值来获取当前线程的 UI 区域性 CultureInfo.CurrentUICulture ,如下面的示例所示。
using System;
using System.Globalization;
public class Example
{
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 Example
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]
还可以从属性中检索当前线程的 UI 区域性的值 Thread.CurrentUICulture 。
显式设置当前 UI 区域性
从 .NET Framework 4.6 开始,可以通过将 CultureInfo 表示新区域性的对象分配给属性,来更改当前 UI 区域性 CultureInfo.CurrentUICulture 。 当前 UI 区域性可以设置为特定区域性 (例如 en-us 或取消) ,或 (如 en 或 de) 之类的非特定区域性。 下面的示例将当前 UI 区域性设置为 fr-fr 或法语 (法国) 。
using System;
using System.Globalization;
public class Example
{
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
Imports System.Threading
Module Example
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 表示区域性的对象分配给线程的属性来显式设置任何线程的 UI 区域性 Thread.CurrentUICulture 。 如果要设置其区域性的线程是当前线程,则可以将新区域性分配给该 CultureInfo.CurrentUICulture 属性。 当显式设置线程的 UI 区域性时,即使该线程跨越应用程序域边界并在另一个应用程序域中执行代码,它也会保留相同的区域性。
隐式设置当前 UI 区域性
第一次创建线程(包括主应用程序线程)时,默认情况下它的当前 UI 区域性设置如下:
DefaultThreadCurrentUICulture如果属性值不为,则使用当前应用程序域的属性定义的区域性
null
。使用系统的默认区域性。 在使用 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 区域性。 和 CurrentCulture CurrentUICulture 属性映射到属性中的第一Windows。ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages集合。
在 .NET Framework 和 .NET Core 应用中,当前 UI 区域性是按线程设置的,并且 属性仅反映当前线程 CurrentUICulture 的 UI 区域性。 在 UWP 应用中,当前区域性映射到Windows。ApplicationModel.Resources.Core.ResourceManager.DefaultContext.Languages属性,这是一个全局设置。 设置 CurrentCulture 属性会更改整个应用的区域性;不能基于每个线程设置区域性。