所以我有一个应用程序,其样式直接放入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
非常感谢任何帮助。谢谢!
答案 0 :(得分:4)
你读的是正确的。
您需要的是在共享程序集内部使用给定名称创建普通ResourceDictionary
。
在App.xaml
中,您可以将此ResourceDictionary
作为MergedDictionary
加入,因此您的整个应用都可以访问所有共享词典资源。
步骤:
<强> 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>
App.xaml
参考Dictionary1.xaml
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>