CultureInfo.CurrentUICulture 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定代表當前使用者介面文化的 CultureInfo 物件,該物件由Resource Manager在執行時用來查詢特定文化資源。
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; }
static member CurrentUICulture : System.Globalization.CultureInfo with get, set
static member CurrentUICulture : System.Globalization.CultureInfo
Public Shared Property CurrentUICulture As CultureInfo
Public Shared ReadOnly Property CurrentUICulture As CultureInfo
屬性值
Resource Manager 用來在執行時查詢特定文化資源的文化。
例外狀況
屬性設定為 null。
該屬性被設定為無法用來定位資源檔案的文化名稱。 資源檔名可以只包含字母、數字、連字號或底線。
範例
以下程式碼範例示範如何更改 CurrentCulture 目前執行緒的 and CurrentUICulture 。
using System;
using System.Globalization;
public class Example0
{
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 Example2
Public Sub Run()
' 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.
備註
CurrentUICulture 屬性是每個線程的屬性。 也就是說,每個線程都有自己的目前UI文化特性。 這個屬性相當於擷取或設定指派給 CultureInfo 屬性的 System.Threading.Thread.CurrentThread.CurrentUICulture 物件。 啟動線程時,其UI文化特性一開始會決定如下:
藉由擷取執行線程的應用程式域中由 DefaultThreadCurrentUICulture 屬性指定的文化特性,若屬性值不是
null。如果執行緒是一個執行基於任務的非同步操作的執行緒池執行緒,其 UI 文化由呼叫執行緒的 UI 文化決定。 下列範例會將目前的UI文化特性變更為葡萄牙文(巴西),並啟動六項工作,其中每個工作都會顯示其線程標識碼、工作標識元,以及目前的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 {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 {Thread.CurrentThread.CurrentUICulture.Name}"); Console.WriteLine($"Application thread is thread {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 {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {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-BRImports 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 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 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 UI culture is en-US ' Current UI 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如需詳細資訊,請參閱 文化和基於任務的異步操作。
藉由呼叫 Windows
GetUserDefaultUILanguage函式。
若要變更線程所使用的使用者介面文化特性,請將 Thread.CurrentUICulture 屬性設定為新的文化特性。 如果您以這種方式明確變更執行緒的 UI 文化特性,當該執行緒跨越應用程式域界限時,該變更會持續有效。
Note
如果您將屬性值設定為代表新文化特性的 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 {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.CurrentUICulture
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文化特性
你可以透過指 CultureInfo 派一個代表新文化的物件來 CultureInfo.CurrentUICulture 更改目前的 UI 文化。 目前的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: {CultureInfo.CurrentUICulture.Name}");
CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"The current UI culture: {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文化特性,如下所示:
- 如果 DefaultThreadCurrentUICulture屬性值不存在,則使用目前應用程式域
null屬性所定義的文化特性。 - 使用系統的預設文化特性。 在使用 Windows 作業系統的電腦上,Common Language Runtime(通用語言執行緒)會呼叫 Windows
GetUserDefaultUILanguage函式來設定目前的 UI 文化。GetUserDefaultUILanguage會傳回使用者設定的預設UI文化特性。 如果使用者尚未設定預設UI語言,則會傳回原本安裝在系統上的文化特性。
如果線程跨越應用程式界限,並在另一個應用程式域中執行程序代碼,則會以與新建立線程相同的方式判斷其文化特性。
請注意,如果您設定與系統安裝的UI文化特性或使用者慣用UI文化特性不同的特定UI文化特性,而且您的應用程式會啟動多個線程,除非您將文化特性指派給線程執行所在的應用程式域中的 GetUserDefaultUILanguage 屬性,否則這些線程目前的UI文化特性將會是 DefaultThreadCurrentUICulture 函式所傳回的文化特性。
安全性考慮
變更目前線程的文化特性需要擁有 SecurityPermission 許可權,且該許可權必須設定為 ControlThread 值。
Caution
操作線程很危險,因為線程的安全狀態。 因此,此許可權應該只提供給可信任的程式代碼,然後才視需要提供。 您無法在半信任的程式代碼中變更線程文化特性。