0

我在 WPF 中创建了一个本地化应用程序。我已经像这样绑定了xaml中的控件。

<Label Name="languageLabel" Content="{x:Static Resources:Resources.languageLabel}" Style="{StaticResource CommonRepSecStyle}" />

一切正常,但我仍然不确定我应用的技术是否良好。我创建了一个资源管理器并将字符串值分配给这样的控件。

        Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
        Dim localResource As System.Resources.ResourceManager = New System.Resources.ResourceManager("LocalTest.Resources", System.Reflection.Assembly.GetExecutingAssembly())

        languageLabel.Content = localResource.GetString("languageLabel", culture)
       stateLabel.Content = localResource.GetString("stateLabel", culture)
        cityLabel.Content = localResource.GetString("cityLabel", culture)
       stateComboBox.Text = localResource.GetString("stateComboBox", culture)
        cityComboBox.Text = localResource.GetString("cityComboBox", culture)
        infoLabel.Content = localResource.GetString("infoLabel", culture)
        infoTextBox.Text = localResource.GetString("infoTextBox", culture)
        localResources.ReleaseAllResources()

在我将值分配给控件之前,它似乎不起作用?有什么办法可以改善这一点吗?谢谢!

编辑:完整的代码在这里

    Imports System.IO
    Imports System.Windows.Controls
    Imports System.Globalization
    Imports System.Configuration
    Imports System.Threading
    Imports System.ComponentModel
    Imports System.Resources
    Imports System.Windows.Markup
    Imports System.Reflection
    Imports System.Uri
    Namespace LocalTest

Public Class MainWindow
    Private cultureName As String


    Public Sub New()

        '    '    ' This call is required by the designer.
        InitializeComponent()

        '    '    ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded


    End Sub
    Private Sub ButtonEnglish_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonEnglish.Click
        cultureName = "en-US"

        SetControlsCulture()
        'Dim ob As Object
        'ob = Application.Current.TryFindResource("Resources.en-US.resx")

        'Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
        'Thread.CurrentThread.CurrentUICulture  = CultureInfo.CreateSpecificCulture("en-US")

    End Sub

    Private Sub ButtonFrench_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonFrench.Click
        cultureName = "fr-CA"
        System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-CA")

        SetControlsCulture()

    End Sub
    ' Dim s As String = r.GetString("LabelContent", culture)
    'Thread.CurrentThread.CurrentCulture = culture
    'Thread.CurrentThread.CurrentUICulture = culture


    ''' <summary>
    ''' Select the Controls Culture
    ''' </summary>
    ''' <remarks></remarks>

    Private Sub SetControlsCulture()
        Select Case cultureName
            Case "fr-CA"
                UIControlsBinding()
            Case "en-US"
                UIControlsBinding()
        End Select
    End Sub
   Private Sub UIControlsBinding()
        Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
        Dim localResource As System.Resources.ResourceManager = New      System.Resources.ResourceManager("LocalTest.Resources", System.Reflection.Assembly.GetExecutingAssembly())
     languageLabel.Content = localResource.GetString("languageLabel", culture)
     stateLabel.Content = localResource.GetString("stateLabel", culture)
     cityLabel.Content = localResource.GetString("cityLabel", culture)
     stateComboBox.Text = localResource.GetString("stateComboBox", culture)
     cityComboBox.Text = localResource.GetString("cityComboBox", culture)
    infoLabel.Content = localResource.GetString("infoLabel", culture)
    infoTextBox.Text = localResource.GetString("infoTextBox", culture)
    localResources.ReleaseAllResources() 
    End Sub
    End class
    End Namespace
4

1 回答 1

1

要在 WPF 中添加资源,请将一个Resource.resx和一个Resource.NL-nl.resx(或选择的语言)文件添加到项目的属性文件夹中。在每个资源文件中添加一个具有名称StateLabel和值的资源。不要忘记将访问修饰符更改为公共

将 xml 命名空间添加到您的窗口/用户控件:(xmlns:Resources="clr-namespace:WpfApplication2.Properties"其中 WpfApplication2 是您的应用程序名称)

为您的窗口/用户控件添加标签<Label Content="{x:Static Resources:Resources.StateLabel}"></Label>

要更改语言,请从代码中调用它以设置适当的语言(在本例中为荷兰语):

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
于 2013-03-04T21:21:55.140 回答