2

我有一个库 ( Styles.DLL),其中包含一个键控 WPF 的集合Styles

我有一个类库 ( Module.DLL),其中包含许多Windows并且UserControls可以在各种应用程序之间共享。Styles我使用定义的 keyed为这些上使用的各种Styles.DLL创建隐式,例如or 。StylesControlsWindowsUserControlsButtonComboBox

然后我有一个应用程序 ( App.EXE),我在其中使用Windows定义的Module.DLL. 我将所需的ResourceDictionariesfrom合并Styles.DLLApp.xaml.App.EXE

这一切都有效。


我的问题是:如何在托管应用程序中删除从 App.xaml 合并的字典并将其包含在 Module.DLL 中,而不必将字典合并到每个 Window 的资源中?

我想我正在寻找类似app.xaml文件但对于类库的东西......

4

2 回答 2

3

我使用一种清除当前字典并合并我想要的字典的方法。实际上,“清除”必须在方法调用中设置,这里是一个例子:

    void AddResourceDictionary(string source)
    {
        ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri(source, UriKind.RelativeOrAbsolute)) as ResourceDictionary;
        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
    }

实现自定义主题/皮肤/资源:

        private void ThemeNameHere()
    {
        Application.Current.Resources.Clear();

        AddResourceDictionary("Computar.Wpf;component/Style/MyStyle.xaml");
        ....

    }

这非常有帮助!

于 2013-10-10T11:43:10.767 回答
1

我找不到解决这个问题的方法XAML。我的解决方案是使用以下代码将所需ResourceDictionariesApplication对象合并到代码中:

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

希望这会帮助有类似问题的人。

于 2013-10-10T09:45:56.447 回答