【问题标题】:Multilanguage wpf application with resources in Visual Studio Designer具有 Visual Studio 设计器中资源的多语言 wpf 应用程序
【发布时间】:2015-07-23 10:02:05
【问题描述】:

这是我的问题: 我有多语言 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 文件?

【问题讨论】:

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


    【解决方案1】:

    你是在正确的方式。

    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}" />
      

    它对我有用。希望对您有所帮助。

    【讨论】:

    • 它不起作用,因为我认为 app.xaml 中的这个资源文件会覆盖 app.xaml.cs 中的这个资源文件。我在 application.Run() 之前调用此方法,并且在该方法中我的合并字典很清楚。现在,当我设置英语文化时,我有德语资源(来自 app.xaml)
    • 您可以处理应用程序的启动方法。它在调用 Run 方法后引发。在 Startup 处理程序中执行与 Run() 之前完全相同的操作
    • 好的,现在可以了,但是在设计师中我现在总是有德国资源。我关心改变这取决于文化。你知道怎么做吗?
    • 好的,加载设计器时会调用您的窗口构造函数。您可以在构造函数中更改文化并调用替换 ResourceDictionaries 的方法。您只能在设计时使用 if (DesignerProperties.GetIsInDesignMode(this)) { /*change culture and replace dictionaries*/} 但我认为它不会起作用。因此,答案是否定的,您将无法在设计时更改字典,具体取决于您的文化。您可以使用 WPF Infralution。很不错,符合你的要求。
    • 此解决方案不起作用,我会检查 infralution,但感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多