Thread.CurrentUICulture Egenskap

Definition

Hämtar eller anger den aktuella kultur som används av Resource Manager för att söka efter kulturspecifika resurser vid körning.

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

Egenskapsvärde

Ett objekt som representerar den aktuella kulturen.

Undantag

Egenskapen är inställd på null.

Egenskapen är inställd på ett kulturnamn som inte kan användas för att hitta en resursfil. Resursfilnamn får endast innehålla bokstäver, siffror, bindestreck eller understreck.

.NET Core och endast .NET 5+ : Det går inte att läsa eller skriva en tråds kultur från en annan tråd.

Exempel

I följande exempel avgörs om språket i den aktuella trådens användargränssnittskultur är franska. Om den inte är det anger den användargränssnittskulturen för den aktuella tråden till engelska (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]
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]

I följande kodexempel visas trådningssatsen som gör att användargränssnittet för en Windows Forms kan visas i den kultur som anges i ovládací panel. Ytterligare kod krävs.

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

Kommentarer

Användargränssnittskulturen anger de resurser som ett program behöver för att stödja användarindata och utdata och är som standard samma som operativsystemkulturen. CultureInfo Se klassen för att lära dig mer om kulturnamn och identifierare, skillnaderna mellan invarianta, neutrala och specifika kulturer och hur kulturinformation påverkar trådar och programdomäner. CultureInfo.CurrentUICulture Se egenskapen för att lära dig hur en tråds standardkultur för användargränssnittet bestäms.

Important

Egenskapen CurrentUICulture fungerar inte tillförlitligt när den används med någon annan tråd än den aktuella tråden. I .NET Framework är det tillförlitligt att läsa egenskapen, men det är inte att ange den för en annan tråd än den aktuella tråden. På .NET Core genereras en InvalidOperationException om en tråd försöker läsa eller skriva egenskapen CurrentUICulture på en annan tråd. Vi rekommenderar att du använder CultureInfo.CurrentUICulture egenskapen för att hämta och ange den aktuella kulturen.

Den CultureInfo som returneras av den här egenskapen kan vara en neutral kultur. Neutrala kulturer ska inte användas med formateringsmetoder som String.Format(IFormatProvider, String, Object[]), DateTime.ToString(String, IFormatProvider)och Convert.ToString(Char, IFormatProvider). CultureInfo.CreateSpecificCulture Använd metoden för att hämta en specifik kultur eller använda egenskapen CurrentCulture .

Gäller för