具有Visual Studio Designer资源的多语言wpf应用程序

时间:2015-07-23 10:02:06

标签: c# wpf xaml visual-studio-designer

这是我的问题: 我有多语言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 Designer中看到这些资源。

另一方面,当我在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文件?

1 个答案:

答案 0 :(得分: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}" />
    
  4. 它对我有用。希望它有所帮助。