2015-05-05 50 views
1

我试图把我的WPF风格在一个单独的库,但在一点点失去,如何这是最好的实现。这是我迄今所做:如何创建自定义WPF样式库

  1. 创建一个新的类库项目并添加引用 PresentationCorePresentationFrameworkSystem.XamlWindowsBase
  2. 创建文件“ MyStyles.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="MyButtonStyle"> 
         <Setter Property="Background" Value="Transparent"/> 
        </Style> 
    </ResourceDictionary> 
    
  3. 建项目。

  4. 创建一个新的WPF应用程序项目,并引用上述建库 。

  5. 在App.xaml中试图引用资源字典中libarary如下:

    `<ApplicationResources> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
         <ResourceDictionary Source="pack://application:,,,/MyCustomWpfStyles;component/MyStyles.xaml"/> 
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
    

    `

  6. 此时VS智能感知告诉,虽然寻找资源时发生错误 字典,尽管应用程序 构建没有问题。

  7. 即使我能够加载资源字典,我不知道如何 与控制使用它,例如,<Button Style="What goes here??" />

看着在互联网上,但似乎无法找到一个如何将样式打包成单独的dll的好例子。任何指针?

回答

2
  1. 不要盲目信任VS智能感觉,随时给你正确的错误信息。有可能是错误的,但我已经看到VS在任何情况下都无法在同一个解决方案中处理多个项目。现在忽略,如果它只是一个警告。如果它是正确的错误,这是不能编译,生成控件库在一个单独的解决方案,并设置适当的参考客户端应用程序的DLL。

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

也看看ResourceDictionary in a separate assembly它说明了如何在其他类型的组件和其他地方使用的资源。

这里是代码,我的机器上工作:

在一个类库项目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属性时可能无法工作。 WPF在Windows 7上使用的花式渐变按钮模板并不是那样做的,而是Windows 8上的平坦渐变按钮模板。1确实。这就是为什么我添加了一个很大的红色边框,以便部分显示风格。

在另一个解决方案,WPF应用程序与以前的DLL的引用(而不是项目)

<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> 
+0

此外,如果您希望所有按钮具有您定义的样式,只需省略“x:Key”属性即可。 – Gimly

+0

谢谢。尽管这里依然没有喜乐。在构建应用程序时,无论我将MyStyles.xaml中的背景属性设置为什么颜色,我似乎都无法在WPF应用程序中获取它。不能帮助我错过简单的东西 - 你知道如何做到这一点的好例子吗? –

+0

@JamesB - 我在回答中添加了一个工作示例。确保所有路径和名称都正确。 –