1

这是我的问题:我有多语言 WPF 应用程序,其资源位于两个不同的文件中。现在我在 app.xaml.cs 中选择正确的一个,如下所示:

var dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
    case "de-DE":
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
        break;
    default:
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
        break;
}
Resources.MergedDictionaries.Add(dict);

一切正常,但我在 VisualStudio 设计器中看不到该资源。

另一方面,当我在 App.xaml 文件中定义 ResourceDictionary 时,如下所示:

<Application x:Class="Ampe.UI.Views.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

然后我在设计器中有这些资源,但我无法设置多语言。

在具有多语言应用程序的设计器中是否有可能看到资源?应用程序打开时可能会更改 app.xaml 文件?

4

1 回答 1

2

你走对了。

  1. 我建议您在添加新字典之前清除应用程序合并字典。

        Resources.MergedDictionaries.Clear();
        var dict = new ResourceDictionary();
        switch (Thread.CurrentThread.CurrentCulture.ToString())
        {
            case "de-DE":
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
                break;
            default:
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
                break;
        }
        Resources.MergedDictionaries.Add(dict);
    
  2. 你的 app.xaml 应该看起来像你说的:

    <Application x:Class="Ampe.UI.Views.App"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    
  3. 当您从资源中获取本地化值时,您必须使用 DynamicResources 而不是 StaticResources:

    <TextBlock Text="{DynamicResource MyString}" />
    

这个对我有用。希望能帮助到你。

于 2015-07-23T10:39:37.987 回答