2015-04-24 129 views
1

所以我有一个风格直接放到App.xaml文件为这样的应用:便携式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项目都可以在设计器中看到“SpecialButtonStyle”作为可选的“样式”。 我读过几篇关于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

任何帮助将不胜感激。谢谢!

回答

4

你读的是正确的。

您需要的是在共享程序集内创建一个带有给定名称的普通ResourceDictionary

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

步骤:

  1. 创建另一个项目的WPF控件库项目(不要紧有关用户或自定义)
  2. 在新项目中右键 - >添加 - > ResourceDictionary中
  3. 贴上自己的风格在这样它看起来像下面这样:

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

    谢谢!一旦我有机会实施这一点,我会尽快回复。 PS。我不能upVote,直到我有15名声望。 – TaterJuice

    +0

    太棒了。得到它的工作,这是答案! – TaterJuice

    +0

    很高兴帮助:-) –