我正在尝试将我的WPF样式放在一个单独的库中,但是我有点不知道应该如何最好地实现。到目前为止,我已经完成了以下步骤:
- Created a new Class Library project and added references PresentationCore, PresentationFramework, System.Xaml and WindowsBase
Created file "MyStyles.xaml" in this class library project with the following content:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="Button" x:Key="MyButtonStyle"> <Setter Property="Background" Value="Transparent"/> </Style> </ResourceDictionary>
Built the project.
Created a new WPF application project, and referenced the library built above.
In App.xaml attempted to reference the resource dictionary in the libarary as follows:
`<ApplicationResources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MyCustomWpfStyles;component/MyStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
`
At this point VS intellisense is telling that an error occurred while finding the resource dictionary, although the application builds without problem.
Even if I am able to load the resource dictionary, I'm not sure how to use it with a control, e.g.,
<Button Style="What goes here??" />
在互联网上查找了一下,但好像找不到如何将样式打包成单独的dll的好例子。有什么建议吗?
Application.Resources
从Project B添加资源。 Project C还通过StartupUri
(Package Syntax)启动Project A的MainWindow.xaml。实际上,设计师大多无法正确解析这些资源并显示警告。结果是,您无法正确使用设计师,因为来自资源的样式和图像未呈现。当应用程序被执行时,一切正常。在我看来,这不值得努力。只需简化您的依赖关系即可。 - LuckyLikey