在特定文件夹中找不到ResourceDictionary

时间:2018-01-25 11:16:03

标签: c# wpf xaml

我是这个应用程序结构:

AppName
    Resources
        Languages
            en.xaml
            it.xaml

我尝试根据应用程序启动时设置的软件的ResourceDictionary加载特定的CurrentCulture,例如:

var dict = new ResourceDictionary();
string currentLang = "it-IT";

if (currentLang == "it-IT")
{
    CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("it");
    Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));
}
else
{
    CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en");
    Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\en.xaml", UriKind.Relative));
}

当代码到达此行时:Application.LoadComponent(dict, new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));

我收到此错误:

  

System.Exception:'由URI'标识的资源。 .. \ Resources \ Languages \ it.xaml'不适用于' System.Windows.ResourceDictionary'组件。'

为什么会发生这种情况?

感谢任何解释。

3 个答案:

答案 0 :(得分:1)

试试这样。我这样做,它找到了它们。

Application.LoadComponent(dict, new Uri("pack:\\application:,,,\YourDLL;\\component\\Resources\\Languages\\en.xaml", UriKind.Relative));

答案 1 :(得分:0)

这应该有效:

string currentLang = "it-IT";

if (currentLang == "it-IT")
{
    CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("it");
    MainWindow.AppWindow.lsVm.GetCurrentLangDict=(ResourceDictionary)Application.LoadComponent(new Uri("..\\Resources\\Languages\\it.xaml", UriKind.Relative));
}
else
{
    CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en");
    MainWindow.AppWindow.lsVm.GetCurrentLangDict=(ResourceDictionary)Application.LoadComponent(new Uri("..\\Resources\\Languages\\en.xaml", UriKind.Relative));
}

答案 2 :(得分:0)

这应该加载资源字典并合并到您的App.xaml

ResourceDictionary dict = new ResourceDictionary() { Source = new Uri("/Resources/Languages/it.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(dict);