代码之家  ›  专栏  ›  技术社区  ›  darson1991

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

  •  1
  • darson1991  · 技术社区  · 11 年前

    这是我的问题: 我有多语言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);
    

    一切都很好,但我在VisualStudioDesigner中看不到这些资源。

    另一方面,当我在App中定义ResourceDictionary时。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>
    

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

    在多语言应用程序的设计器中是否可以看到资源?也许是某种改变应用。应用程序打开时xaml文件?

    1 回复  |  直到 11 年前
        1
  •  2
  •   keymusicman    11 年前

    你走在正确的道路上。

    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. 你的应用。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}" />
      

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