Thread.CurrentUICulture 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置资源管理器用于在运行时查找区域性特定资源的当前区域性。
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
属性值
表示当前区域性的对象。
例外
属性设置为 null.
该属性设置为无法用于查找资源文件的区域性名称。 资源文件名必须仅包含字母、数字、连字符或下划线。
仅 .NET Core 和 .NET 5+ :不支持从另一个线程读取或写入线程的区域性。
示例
以下示例确定当前线程的 UI 区域性的语言是否为法语。 如果不是,它将当前线程的 UI 区域性设置为英语(美国)。
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]
open System.Globalization
open System.Threading
// Change the current culture if the language is not French.
let current = Thread.CurrentThread.CurrentUICulture
if current.TwoLetterISOLanguageName <> "fr" then
let newCulture = CultureInfo.CreateSpecificCulture "en-US"
Thread.CurrentThread.CurrentUICulture <- newCulture
// Make current UI culture consistent with current culture.
Thread.CurrentThread.CurrentCulture <- newCulture
printfn
$"The current UI culture is {Thread.CurrentThread.CurrentUICulture.NativeName} [{Thread.CurrentThread.CurrentUICulture.Name}]"
printfn
$"The current culture is {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 窗体的用户界面显示在控制面板中设置的区域性中。 需要其他代码。
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());
}
}
open System.Threading
open System.Windows.Forms
type UICulture() =
inherit Form()
do
// 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.
new UICulture() |> Application.Run
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 区域性指定应用程序支持用户输入和输出所需的资源,默认情况下与操作系统区域性相同。 请参阅该 CultureInfo 类,了解区域性名称和标识符、固定、中立和特定区域性之间的差异,以及区域性信息影响线程和应用程序域的方式。 请参阅该 CultureInfo.CurrentUICulture 属性,了解如何确定线程的默认 UI 区域性。
重要
与当前线程以外的任何线程一起使用时,该 CurrentUICulture 属性无法可靠地工作。 在 .NET Framework 中,读取属性是可靠的,尽管为当前线程以外的线程设置属性不是。 在 .NET Core 上,如果线程尝试在不同的线程上读取或写入CurrentUICulture属性,InvalidOperationException则会引发该属性。 建议使用 CultureInfo.CurrentUICulture 属性来检索和设置当前区域性。
CultureInfo此属性返回的区域性可以是中性区域性。 中性区域性不应与格式设置方法一起使用,例如 String.Format(IFormatProvider, String, Object[]), DateTime.ToString(String, IFormatProvider)和 Convert.ToString(Char, IFormatProvider)。 CultureInfo.CreateSpecificCulture使用该方法获取特定区域性,或使用CurrentCulture属性。