我有一个用户控件库项目,里面有一些资源字典,用Microsoft Visual Studio 2017创建。资源字典包含XAML样式,我希望团队中的其他开发人员能够在不同的WPF中使用它们 - 应用项目。将用户控件库项目复制到WPF-App项目中并不是一个好的解决方案。 我尝试了以下方法:
UserControleLibraryProjectName/bin/debug/
中的.dll添加到WPF-App中的引用。将使用过的资源dictionarys导入到WPF-App中,方法是将其添加到app.xaml中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UserControleLibraryProjectName;component/ResourceDictionaryName.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在MainWindow.xaml
中照常引用资源字典条目:例如Style="{StaticResource style1}"
。
它编译时没有错误,但在启动时崩溃,因为它无法找到引用的资源。 (System.Windows.Markup.XamlParseException
中的System.Windows.ResourceDictionary.Source
)
我很确定我引用资源字典的源标记的语法是错误的,但我尝试了很多不同的方法......
顺便说一句。用户控件库项目中没有错误,也没有我引用字典条目的MainWindow.xaml
- 当我将资源字典代码直接复制到app.xaml
时,它可以正常工作。
您是否知道如何正确使用引用的dll?
答案 0 :(得分:1)
建议查看这个问题的答案,因为我看到路径看起来很好。
Loading merged ResourceDictionary from different assembly fails
答案 1 :(得分:0)
您的来源看起来正确。它是:
<ResourceDictionary Source="pack://application:,,,/[AssemblyName];component/[PathToFile].xaml" />
您还需要在应用中引用您的ClassLibrary才能使其正常工作。
如果这不起作用,首先,如果您的程序集名称与您在App.xaml中放置的程序集名称相同,请检入项目属性。
答案 2 :(得分:0)
谢谢到目前为止,我发现了问题:
用户控件库项目的<link href="/css/style.css" rel="stylesheet"/>
版本高于WPF-App项目的版本。由于产生的不兼容性,Visual Studio未将.net
复制到WPF-App项目的.dll
文件夹中。
另外,我必须将引用的用户控制库的构建类型设置为bin/debug/
。