CultureInfo.DefaultThreadCurrentUICulture 屬性

定義

取得或設定目前應用程式定義域中之執行緒的預設 UI 文化特性。

public:
 static property System::Globalization::CultureInfo ^ DefaultThreadCurrentUICulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public static System.Globalization.CultureInfo DefaultThreadCurrentUICulture { get; set; }
public static System.Globalization.CultureInfo? DefaultThreadCurrentUICulture { get; set; }
static member DefaultThreadCurrentUICulture : System.Globalization.CultureInfo with get, set
Public Shared Property DefaultThreadCurrentUICulture As CultureInfo

屬性值

在目前的應用程式定義域中為執行緒的預設 UI 文化特性,若目前的系統 UI 文化特性是應用程式定義域中的預設執行緒 UI 文化特性,則為 null

例外狀況

設定作業中,Name 屬性值無效。

範例

下列範例說明定義新線程目前文化特性時,.NET Framework 的默認行為。 它會使用英文和俄文語言資源。 下列名為 GreetingStrings.txt 的文字檔包含英文語言資源:

greeting =Hello again!
newGreeting=Hello!

它會使用 資源文件產生器 搭配下列命令,編譯成名為 GreetingStrings.resources 的二進位 .resources 檔案。

resgen greetingstrings.txt

下列名為 GreetingStrings.ru-RU.txt 的文字檔包含俄文語言資源:

greeting=Еще раз привет!
newGreeting=Привет!

它會使用 資源文件產生器 搭配下列命令,編譯成名為 GreetingStrings.ru-RU.resources 的二進制 .resources 檔案。

resgen greetingstrings.ru-RU.txt

如下所示的應用程式程式代碼位於名為 Example1.vb 或 Example1.cs 的檔案中。 它會使用 Visual Basic 編譯程式的下列命令編譯成可執行檔:

vbc Example1.vb /resource:GreetingStrings.resources

針對 C# 編譯程式,命令類似:

csc /resource:GreetingStrings.resources Example1.cs

這會建立元件,其中包含範例的可執行程序代碼,以及其後援文化特性的資源。 您也可以使用 元件連結器 ,使用下列命令建立俄文 (俄羅斯) 文化特性的資源檔:

>al /embed:greetingstrings.ru-RU.resources /c:ru-RU /template:example1.exe /out:ru-RU\Example1.resources.dll

在啟動時,此範例會將目前文化特性和目前UI文化特性設定為俄文 (俄羅斯在所有系統上) ,但預設系統文化特性已經是俄文 (俄羅斯) 。 如果預設系統文化特性已經是俄文 (俄羅斯) ,則程式代碼會將目前的文化特性和目前的UI文化特性設定為英文 (美國) 。 然後它會呼叫 ShowGreeting 例程,它會在第一次呼叫時顯示簡單的字串,並在後續方法呼叫時略有不同字串。 接下來,它會建立新的線程,也會執行 ShowGreeting 例程。

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Hello again!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Hello again!

如範例輸出所示,當範例在系統文化特性為英文 (美國) 的計算機上執行時,主線程會在俄文中顯示其初始字串。 不過,因為背景工作線程的文化特性衍生自目前的 Windows 系統文化特性,而不是應用程式的目前文化特性,所以背景工作線程會以英文顯示其字串。

下列範例會使用 DefaultThreadCurrentCultureDefaultThreadCurrentUICulture 屬性來定義新應用程式線程的目前文化特性和目前 UI 文化特性。

此範例會使用與上一個範例相同的資源檔。 編譯及將當地語系化俄文語言資源內嵌至附屬元件的命令也相同,不同之處在於可執行檔元件的名稱會變更。

在啟動時,此範例會將目前文化特性和目前UI文化特性設定為俄文 (俄羅斯在所有系統上) ,但預設系統文化特性已經是俄文 (俄羅斯) 。 如果預設系統文化特性已經是俄文 (俄羅斯) ,則會將目前的文化特性和目前的UI文化特性設定為英文 (美國) 。 然後它會呼叫 ShowGreeting 例程,它會在第一次呼叫時顯示簡單的字串,並在後續方法呼叫時略有不同字串。 接下來,它會建立新的線程,也會執行 ShowGreeting 例程。

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Еще раз привет!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      CultureInfo.DefaultThreadCurrentCulture = culture
      CultureInfo.DefaultThreadCurrentUICulture = culture
       
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Еще раз привет!

如範例輸出所示,當範例在系統文化特性為英文 (美國) 的計算機上執行時,主線程和背景工作線程都會以俄文語言顯示其字串。

備註

根據預設,在 .NET Framework 4 和舊版中,所有線程的 UI 文化特性都會設定為 Windows 系統文化特性。 對於目前UI文化特性與默認系統文化特性不同的應用程式,通常不想要這種行為。 在 .NET Framework 4.5 中DefaultThreadCurrentUICulture,屬性可讓您定義應用程式域中所有線程的預設 UI 文化特性。

重要

如果您尚未明確設定在應用程式域中執行之任何現有線程的UI文化特性,則設定 DefaultThreadCurrentUICulture 屬性也會變更這些線程的文化特性。 不過,如果這些線程在另一個應用程式域中執行,則其文化特性是由 DefaultThreadCurrentUICulture 該應用程式域中的屬性所定義,或者如果未定義預設值,則默認系統文化特性會定義它們。 因此,建議您一律明確設定主要應用程式線程的文化特性,而不依賴 DefaultThreadCurrentUICulture 屬性來定義主要應用程式線程的文化特性。

除非明確設定此屬性,否則屬性的值 DefaultThreadCurrentUICulturenull,而且尚未指派明確文化特性的應用程式域中所有線程的目前文化特性都是由預設 Windows 系統文化特性所定義。

如需文化特性、線程和應用程式域的詳細資訊,請參閱參考頁面中的和一節 CultureInfo

適用於

另請參閱