Thread.CurrentUICulture 属性

定义

获取或设置资源管理器使用的当前区域性以便在运行时查找区域性特定的资源。Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.

public:
 property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public System.Globalization.CultureInfo CurrentUICulture { get; set; }
member this.CurrentUICulture : System.Globalization.CultureInfo with get, set
Public Property CurrentUICulture As CultureInfo

属性值

表示当前区域性的对象。An object that represents the current culture.

异常

属性设置为 nullThe property is set to null.

该属性设置为不能用于定位资源文件的区域性名称。The property is set to a culture name that cannot be used to locate a resource file. 资源文件名必须仅包含字母、数字、连字符或下划线。Resource filenames must include only letters, numbers, hyphens or underscores.

仅限 .NET Core:不支持从一个线程读取或写入另一个线程的区域性。.NET Core only: Reading or writing the culture of a thread from another thread is not supported.

示例

下面的示例确定当前线程的 UI 区域性的语言是否为法语。The following example determines whether the language of the current thread's UI culture is French. 如果不是,则将当前线程的 UI 区域性设置为 "英语(美国)"。If it is not, it sets the UI culture of the current thread to English (United States).

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Change the current culture if the language is not French.
      CultureInfo current = Thread.CurrentThread.CurrentUICulture;
      if (current.TwoLetterISOLanguageName != "fr") {
         CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
         Thread.CurrentThread.CurrentUICulture = newCulture;
         // Make current UI culture consistent with current culture.
         Thread.CurrentThread.CurrentCulture = newCulture;
      }
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name);
      Console.WriteLine("The current culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name);
   }
}
// The example displays the following output:
//     The current UI culture is English (United States) [en-US]
//     The current culture is English (United States) [en-US]
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Change the current culture if the language is not French.
      Dim current As CultureInfo = Thread.CurrentThread.CurrentUICulture
      If current.TwoLetterISOLanguageName <> "fr" Then
         Dim newCulture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
         Thread.CurrentThread.CurrentUICulture = newCulture
         ' Make current UI culture consistent with current culture.
         Thread.CurrentThread.CurrentCulture = newCulture
      End If
      Console.WriteLine("The current UI culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name)
      Console.WriteLine("The current culture is {0} [{1}]",
                        Thread.CurrentThread.CurrentUICulture.NativeName,
                        Thread.CurrentThread.CurrentUICulture.Name)
   End Sub
End Module
' The example displays output like the following:
'     The current UI culture is English (United States) [en-US]
'     The current culture is English (United States) [en-US]

下面的代码示例演示了一个线程语句,该语句允许 Windows 窗体的用户界面显示在 "控制面板" 中设置的区域性。The following code example shows the threading statement that allows the user interface of a Windows Forms to display in the culture that is set in Control Panel. 需要其他代码。Additional code is needed.

#using <system.dll>
#using <System.Drawing.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Windows::Forms;
ref class UICulture: public Form
{
public:
   UICulture()
   {
      
      // Set the user interface to display in the
      // same culture as that set in Control Panel.
      Thread::CurrentThread->CurrentUICulture = Thread::CurrentThread->CurrentCulture;
      
      // Add additional code.
   }
};


int main()
{
   Application::Run( gcnew UICulture );
}
using System;
using System.Threading;
using System.Windows.Forms;

class UICulture : Form
{
    public UICulture()
    {
        // Set the user interface to display in the
        // same culture as that set in Control Panel.
        Thread.CurrentThread.CurrentUICulture = 
            Thread.CurrentThread.CurrentCulture;

        // Add additional code.
    }

    static void Main()
    {
        Application.Run(new UICulture());
    }
}
Imports System.Threading
Imports System.Windows.Forms

Public Class UICulture : Inherits Form
    Sub New()

        ' Set the user interface to display in the
        ' same culture as that set in Control Panel.
        Thread.CurrentThread.CurrentUICulture = _
            Thread.CurrentThread.CurrentCulture

        ' Add additional code.
    End Sub

    Shared Sub Main()
        Application.Run(New UICulture())
    End Sub
End Class

注解

UI 区域性指定应用程序支持用户输入和输出所需的资源,默认情况下,它与操作系统区域性相同。The UI culture specifies the resources an application needs to support user input and output, and by default is the same as the operating system culture. 请参阅 CultureInfo 类,了解区域性名称和标识符、固定、非特定区域性和特定区域性之间的差异,以及区域性信息影响线程和应用程序域的方式。See the CultureInfo class to learn about culture names and identifiers, the differences between invariant, neutral, and specific cultures, and the way culture information affects threads and application domains. 请参阅 CultureInfo.CurrentUICulture 属性,了解如何确定线程的默认 UI 区域性。See the CultureInfo.CurrentUICulture property to learn how a thread's default UI culture is determined.

重要

与当前线程以外的任何线程一起使用时,CurrentUICulture 属性不能可靠地工作。The CurrentUICulture property doesn't work reliably when used with any thread other than the current thread. 在 .NET Framework 中,读取属性是可靠的,但对于除当前线程之外的其他线程,其设置是可靠的。In .NET Framework, reading the property is reliable, although setting it for a thread other than the current thread is not. 在 .NET Core 中,如果某个线程尝试在另一个线程上读取或写入 CurrentUICulture 属性,则会引发 InvalidOperationExceptionOn .NET Core, an InvalidOperationException is thrown if a thread attempts to read or write the CurrentUICulture property on a different thread. 建议使用 CultureInfo.CurrentUICulture 属性检索和设置当前区域性。We recommend that you use the CultureInfo.CurrentUICulture property to retrieve and set the current culture.

此属性返回的 CultureInfo 可以是非特定区域性。The CultureInfo returned by this property can be a neutral culture. 非特定区域性不应与格式设置方法一起使用,例如 String.Format(IFormatProvider, String, Object[])DateTime.ToString(String, IFormatProvider)Convert.ToString(Char, IFormatProvider)Neutral cultures should not be used with formatting methods such as String.Format(IFormatProvider, String, Object[]), DateTime.ToString(String, IFormatProvider), and Convert.ToString(Char, IFormatProvider). 使用 CultureInfo.CreateSpecificCulture 方法可获取特定区域性,或使用 CurrentCulture 属性。Use the CultureInfo.CreateSpecificCulture method to get a specific culture, or use the CurrentCulture property.

备注

CultureInfo.CreateSpecificCulture 方法将引发非特定区域性 "zh-chs-Zh-hant" ("zh-chs-CHT")和 "zh-chs-Hans" ("zh-chs")的 ArgumentExceptionThe CultureInfo.CreateSpecificCulture method throws ArgumentException for the neutral cultures "zh-Hant" ("zh-CHT") and "zh-Hans" ("zh-CHS").

适用于