0
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;
            }   
        }
    }

首先我合并了所有字典,然后我尝试从字典中动态获取值,如下所示。但它显示错误(未找到'MyString'资源)。

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

2 回答 2

0

对于英语,它显示 de-DE。你是复制粘贴的受害者吗?

不确定 WPF 但在 silverlight Application.Current.Resources["MyString"] 中有效。

除此之外,您还可以添加到 Application.Resources.MergedDictionaries 中,这样 Application.Resources.MergedDictionaries["My String"] 也可能对您有用。

也许 FindResource 只是寻找 Application.Current.Resources ?

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

于 2012-12-10T07:21:07.780 回答
0
  • 我怀疑Application.Current它是否可以在App's 构造函数中使用,因为Application.Current它应该最终成为App它自己。尝试this.Resources.MergedDictionaries.Add改用。
  • 您的“App.xaml”中有什么东西吗?如果是这样,它将替换App.Resources并且您在构造函数中设置的所有内容都将丢失。
于 2012-12-10T08:23:27.250 回答