.net 自动(无键)样式在另一程序集中的合并词典中不应用
我将样式和模板xaml文件从应用程序(.exe)项目移动到库项目(.dll),因为我想在多个应用程序中使用它们 在App.xaml中,我有:.net 自动(无键)样式在另一程序集中的合并词典中不应用,.net,wpf,.net,Wpf,我将样式和模板xaml文件从应用程序(.exe)项目移动到库项目(.dll),因为我想在多个应用程序中使用它们 在App.xaml中,我有: <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/All.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在All.xaml中(在公共程序集中):
使用此代码,不会应用styles.xaml中的无键样式
相反,如果我在App.xaml中直接引用它们,它会起作用:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Styles.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Templates.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
有人能解释为什么会这样吗?这是资源字典实现的产物。在EXE中,将All.xaml合并到App.xaml后,找不到对其他词典的相对引用。如果您为它们指定完全限定(绝对)包URI,那么您的应用程序将能够找到它们 因此,在All.xaml中替换以下内容:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
<ResourceDictionary Source="Templates.xaml" />
<ResourceDictionary Source="Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
为此:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Styles.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Templates.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Styles.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Templates.xaml" />
<ResourceDictionary Source="pack://application:,,,/MyApplication.Common;component/Resources/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>