Thread.CurrentUICulture Właściwość

Definicja

Pobiera lub ustawia bieżącą kulturę używaną przez Resource Manager do wyszukiwania zasobów specyficznych dla kultury w czasie działania.

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

CultureInfo

Obiekt reprezentujący bieżącą kulturę.

Wyjątki

Właściwość jest ustawiona na null wartość .

Właściwość jest ustawiona na nazwę kultury, która nie może służyć do lokalizowania pliku zasobów. Nazwy plików zasobów muszą zawierać tylko litery, cyfry, łączniki lub podkreślenia.

Tylko .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, kultura interfejsu użytkownika bieżącego wątku jest ustawiana na język 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 wątkową instrukcje, która umożliwia interfejsowi użytkownika aplikacji Windows Forms wyświetlanie 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órych aplikacja potrzebuje do obsługi danych wejściowych i wyjściowych użytkownika, i domyślnie jest taka sama jak kultura systemu operacyjnego. Zapoznaj się z klasą, aby poznać nazwy i identyfikatory kultur, różnice między niezmiennymi, neutralnymi i określonymi kulturami oraz sposób, w jaki informacje o kulturze wpływają na wątki i CultureInfo domeny aplikacji. Zobacz właściwość CultureInfo.CurrentUICulture , aby dowiedzieć się, jak jest określana domyślna kultura interfejsu użytkownika wątku.

Ważne

Właściwość CurrentUICulture nie działa niezawodnie, gdy jest używana z dowolnym wątkiem innym niż bieżący wątek. W .NET Framework odczytywanie właściwości jest niezawodne, chociaż ustawienie jej dla wątku innego niż bieżący wątek nie jest. Na platformie .NET Core jest zgłaszany, jeśli wątek próbuje odczytać lub zapisać właściwość InvalidOperationException CurrentUICulture w innym wątku. Zalecamy użycie właściwości do pobrania CultureInfo.CurrentUICulture i ustawienia bieżącej kultury.

Właściwość CultureInfo zwracana 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) . Użyj CultureInfo.CreateSpecificCulture metody , aby uzyskać określoną kulturę, lub użyj CurrentCulture właściwości .

Dotyczy