我有这个应用程序结构:
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'组件。
为什么会这样?
谢谢你的任何解释。
试试吧。我这样做,它找到了它们。
Application.LoadComponent(dict, new Uri("pack:\\application:,,,\YourDLL;\\component\\Resources\\Languages\\en.xaml", UriKind.Relative));
这应该加载资源字典并合并到您的App.xaml
:
ResourceDictionary dict = new ResourceDictionary() { Source = new Uri("/Resources/Languages/it.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(dict);
这应该工作:
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));
}