如何创建自定义的WPF样式库

4

我正在尝试将我的WPF样式放在一个单独的库中,但是我有点不知道应该如何最好地实现。到目前为止,我已经完成了以下步骤:

  1. Created a new Class Library project and added references PresentationCore, PresentationFramework, System.Xaml and WindowsBase
  2. 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>
    
  3. Built the project.

  4. Created a new WPF application project, and referenced the library built above.

  5. 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>
    

    `

  6. At this point VS intellisense is telling that an error occurred while finding the resource dictionary, although the application builds without problem.

  7. 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的好例子。有什么建议吗?

1个回答

3
  1. 不要盲目相信VS智能提示在所有情况下都能给出正确的错误信息。有时可能会出现问题,但我看过VS无法处理同一解决方案中的多个项目的情况。如果它只是一个警告,请暂时忽略。如果它是一个真正的错误,并且不能编译,请在单独的解决方案中构建控件库,并在客户端应用程序中设置适当的dll引用。

  2. 使用Style="{StaticResource MyButtonStyle}"

还可以查看单独的资源字典,其中介绍了如何在其他类型的程序集和其他位置中使用资源。

这是在我的电脑上运行的代码:

在类库项目WpfControlLibrary1中,在根文件夹中命名为“Dictionary1.xaml”的文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button"
           x:Key="Demo1">
        <Setter Property="BorderBrush"
                Value="Red" />
        <Setter Property="BorderThickness"
                Value="10" />
        <Setter Property="Background"
                Value="Transparent" />
    </Style>
</ResourceDictionary>

请注意,当用于绘制按钮的模板不使用Background属性时,设置Background属性可能无效。WPF在Windows 7上使用的花哨渐变按钮模板并没有这样做,但在Windows 8.1上使用的平面模板则有。这就是为什么我添加了一个大红边框,以便样式部分显示出来。
在另一个解决方案中,一个引用了先前dll(而不是项目)的Wpf应用程序。
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button Content="Button"
                Style="{StaticResource ResourceKey=Demo1}"
                HorizontalAlignment="Left"
                Margin="139,113,0,0"
                VerticalAlignment="Top"
                Width="75" />
    </Grid>
</Window>

将合并的字典移动到应用程序对象也可以起作用:
<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

此外,如果您希望所有按钮都具有您定义的样式,只需省略“x:Key”属性即可。 - Gimly
谢谢。但我还是没有成功。虽然应用程序已经构建完成,但无论我在MyStyles.xaml中将背景属性设置为什么颜色,似乎都无法在我的WPF应用程序中得到它。我可能错过了一些简单的东西 - 你知道有哪些好的例子可以参考吗? - James B
@JamesB - 我在我的回答中添加了一个运行示例。确保所有路径和名称都是正确的。 - Emond
@JamesB - 我还添加了一个关于设置按钮背景的特别说明。一定要阅读。 - Emond
@JamesB - 我添加了一个类似的问题的参考链接,如果程序集(或资源)的类型不同,则可能会解决您的问题。 - Emond
Visual Studio 15的经验:Project C通过Application.ResourcesProject B添加资源。 Project C还通过StartupUri (Package Syntax)启动Project A的MainWindow.xaml。实际上,设计师大多无法正确解析这些资源并显示警告。结果是,您无法正确使用设计师,因为来自资源的样式和图像未呈现。当应用程序被执行时,一切正常。在我看来,这不值得努力。只需简化您的依赖关系即可。 - LuckyLikey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接