类库中的可移植XAML样式

时间:2015-04-24 21:59:41

标签: c# wpf visual-studio xaml portability

所以我有一个应用程序,其样式直接放入App.xaml文件中:

<Application x:Class="Test.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="OnStartup">
    <Application.Resources>
        <Style x:Key="SpecialButtonStyle" TargetType="Button">
            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Block.Foreground" Value="White" />
            <Setter Property="TextBlock.Foreground" Value="White" />
            <Setter Property="TextElement.Foreground" Value="White" />
            <Setter Property="FontWeight" Value="Bold" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}">
                                    <Border Background="{TemplateBinding BorderBrush}">
                                        <ContentControl Foreground="White">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </ContentControl>
                                    </Border>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>

我希望将此作为样式包含在我的类库中,以便引用该库的任何xaml项目都可以看到&#34; SpecialButtonStyle&#34;作为一种可选择的&#34; Style&#34;在设计师。 我已经阅读了几篇关于ResourceDictionaries和创建便携式XAML控件的文章,但我仍然感到困惑。我基本上希望将一组样式作为类库的一部分包含在内。

(我只能发布2个链接,直到获得更高的StackOverflow信誉) http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

http://visualstudiomagazine.com/articles/2015/03/01/everyone-gets-xaml-with-xamarinforms.aspx

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:4)

你读的是正确的。

您需要的是在共享程序集内部使用给定名称创建普通ResourceDictionary

App.xaml中,您可以将此ResourceDictionary作为MergedDictionary加入,因此您的整个应用都可以访问所有共享词典资源。

步骤:

  1. 创建另一个项目WPF控件库项目(与用户或自定义无关)
  2. 在新项目中右键单击 - &gt;添加 - &gt; ResourceDictionary中
  3. 粘贴您的样式,使其如下所示:
  4. <强> Dictionary1.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style x:Key="SpecialButtonStyle" TargetType="Button">
            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Block.Foreground" Value="White" />
            <Setter Property="TextBlock.Foreground" Value="White" />
            <Setter Property="TextElement.Foreground" Value="White" />
            <Setter Property="FontWeight" Value="Bold" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}">
                                    <Border Background="{TemplateBinding BorderBrush}">
                                        <ContentControl Foreground="White">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </ContentControl>
                                    </Border>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
    
    1. 在您的主项目中引用此新控件库
    2. App.xaml参考Dictionary1.xaml
    3. App.xaml

      <Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                       StartupUri="NestedXamlObjects.xaml">
          <Application.Resources>
              <ResourceDictionary>
                  <ResourceDictionary.MergedDictionaries>
                      <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
                  </ResourceDictionary.MergedDictionaries>
              </ResourceDictionary>
          </Application.Resources>
      </Application>