Thread.CurrentUICulture Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera lub ustawia bieżącą kulturę używaną przez Resource Manager do wyszukiwania zasobów specyficznych dla kultury w czasie wykonywania.
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
Wartość właściwości
Obiekt reprezentujący bieżącą kulturę.
Wyjątki
Właściwość jest ustawiona na null.
Właściwość jest ustawiona na nazwę kultury, której nie można użyć do zlokalizowania pliku zasobu. Nazwy plików zasobów muszą zawierać tylko litery, cyfry, łączniki lub podkreślenia.
Tylko platformy .NET Core i .NET 5+: odczytywanie lub zapisywanie kultury wątku z innego wątku nie jest obsługiwane.
Przykłady
Poniższy przykład określa, czy język kultury interfejsu użytkownika bieżącego wątku jest francuski. Jeśli tak nie jest, ustawia kulturę interfejsu użytkownika bieżącego wątku na angielski (Stany Zjednoczone).
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]
Poniższy przykład kodu przedstawia instrukcję wątkowania, która umożliwia interfejs użytkownika Windows Forms wyświetlania w kulturze ustawionej w Panel sterowania. Wymagany jest dodatkowy kod.
#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
Uwagi
Kultura interfejsu użytkownika określa zasoby, które aplikacja musi obsługiwać dane wejściowe i wyjściowe użytkownika, a domyślnie jest taka sama jak kultura systemu operacyjnego. Zobacz klasę CultureInfo , aby dowiedzieć się więcej o nazwach i identyfikatorach kultury, różnicach między niezmiennymi, neutralnymi i określonymi kulturami oraz sposób, w jaki informacje o kulturze wpływają na wątki i domeny aplikacji. Zobacz właściwość , CultureInfo.CurrentUICulture aby dowiedzieć się, jak określana jest domyślna kultura interfejsu użytkownika wątku.
Ważne
Właściwość CurrentUICulture nie działa niezawodnie w przypadku użycia z żadnym wątkiem innym niż bieżący wątek. W .NET Framework odczytywanie właściwości jest niezawodne, chociaż ustawienie go dla wątku innego niż bieżący wątek nie jest. Na platformie .NET Core jest zgłaszany, InvalidOperationException jeśli wątek próbuje odczytać lub zapisać CurrentUICulture właściwość w innym wątku. Zalecamy użycie CultureInfo.CurrentUICulture właściwości do pobrania i ustawienia bieżącej kultury.
Zwracana CultureInfo przez tę właściwość może być kulturą neutralną. Kultury neutralne nie powinny być używane z metodami formatowania, takimi jak String.Format(IFormatProvider, String, Object[]), DateTime.ToString(String, IFormatProvider)i Convert.ToString(Char, IFormatProvider). CultureInfo.CreateSpecificCulture Użyj metody , aby uzyskać określoną kulturę lub użyć CurrentCulture właściwości .