问题合并的ResourceDictionary在WPF

问题描述:

public partial class App : Application 
    { 
     public App() 
     { 
      switch (Thread.CurrentThread.CurrentCulture.ToString()) 
      { 
       case "de-DE": 
        var German = new Uri("..\\GlobalString\\Dictionary_de-DE.xaml", UriKind.RelativeOrAbsolute); 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = German }); 
        break; 
       case "it-IT": 
        var Italy = new Uri("..\\GlobalString\\Dictionary_it-IT.xaml", UriKind.RelativeOrAbsolute); 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = Italy }); 
        break; 
       case "es-ES": 
        var Spanish = new Uri("..\\GlobalString\\Dictionary_es-ES.xaml", UriKind.RelativeOrAbsolute); 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = Spanish }); 
        break; 
       case "fr-FR": 
        var France = new Uri("..\\GlobalString\\Dictionary_fr-FR.xaml", UriKind.RelativeOrAbsolute); 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = France }); 
        break; 
       default: 
        var English = new Uri("..\\GlobalString\\Dictionary_de-DE.xaml", UriKind.RelativeOrAbsolute); 
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = English }); 
        break; 
      } 
     } 
    } 

首先,我合并了所有的字典,然后我试图动态地得到解释像below.But它显示错误(“MyString的”资源未找到)的值。问题合并的ResourceDictionary在WPF

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     string StringFromDictionary= Application.Current.FindResource("MyString"); 
    } 

英文显示de-DE。你是复制粘贴的受害者吗?

不知道为WPF,但在Silverlight Application.Current.Resources [ “MyString的”]的作品。

在这旁边,你加入到Application.Resources.MergedDictionaries所以Application.Resources.MergedDictionaries [“我的字符串”]也可以为你工作。

也许FindResource只是看Application.Current.Resources?

http://msdn.microsoft.com/en-us/library/system.windows.application.findresource(v=vs.100).aspx

  • 我怀疑Application.CurrentApp的构造可用作Application.Current应该最终被App本身。尝试使用this.Resources.MergedDictionaries.Add代替。
  • 您的“App.xaml”中是否有任何内容?如果是这样,它将取代App.Resources,并且您在构造函数中设置的所有内容都将丢失。
+0

我想就像你说的虽然它不是工作。如果我合并字典中Window_Loaded事件意味着,它将工作well.If我按照这个步骤来,我需要合并字典中的所有窗口,在这里我需要从资源字典中获取字符串值。 –