从资源文件中获取一个字符串

时间:2012-12-22 17:51:30

标签: wpf vb.net resourcedictionary

我看了这个样本:

http://www.dotnetfunda.com/articles/article811-simplest-way-to-implement-multilingual-wpf-application.aspx

但是,此示例显示了如何将字符串输入GUI。

如何将字符串输入变量。

我想显示一个消息框,其中包含资源文件中的字符串。

提前致谢 盖

1 个答案:

答案 0 :(得分:1)

您可以使用ResourceDictionary.Item这是修改后的示例代码,以使其按照您的意愿执行。

Class MainWindow
    Dim dict As ResourceDictionary = New ResourceDictionary()
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        SetLanguageDictionary()
        MessageBox.Show(dict.Item("greeting").ToString)
    End Sub

    Private Sub SetLanguageDictionary()

        Select Case (Thread.CurrentThread.CurrentCulture.ToString())
            Case "en-US"
                dict.Source = New Uri("..\Resources\StringResources.xaml", UriKind.Relative)
            Case "fr-CA"
                dict.Source = New Uri("..\Resources\StringResources.fr-CA.xaml", UriKind.Relative)
            Case Else
                dict.Source = New Uri("..\Resources\StringResources.xaml", UriKind.Relative)
        End Select
    End Sub
End Class

和我正在使用的ResourceFile

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system ="clr-namespace:System;assembly=mscorlib" >        
    <system:String x:Key="greeting">Hello World</system:String>
</ResourceDictionary>