动态加载ResourceDictionary

时间:2014-12-23 10:23:24

标签: c# wpf xaml resourcedictionary xamlreader

我有一些问题。我希望我的应用程序在app.xaml.cs中动态加载ResourceDictonnary。

这是我目前的代码:

ResourceDictionary theme = XamlReader.Load(???);
Resources.MergedDictionaries.Add(theme);

问题是,如何从xaml文件中获取我的ResourceDictionnary流?我不想用exe复制xaml文件。它的构建操作设置为Page,我想加载它。

你能告诉我怎么做吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用以下代码(我从我的IModule.Initialize中调用它)

 Application.Current.Resources.MergedDictionaries
            .Add(new ResourceDictionary
            {
                Source = new Uri(@"pack://application:,,,/My.Application;component/Resources/Resources.xaml")
            });

查看附图中的项目树: project tree in VS