2015-02-11 151 views
0

我开始在WPF中使用样式。我使用MahApps风格作为基础,迄今为止非常好。我已经能够使用BasedOn属性进行特定的修改。MahApps风格“找不到资源'MetroCheckBox'”

其中一个较简单的更改是添加默认边距,以便在添加控件时不添加默认边距。直到我尝试使用MetroCheckBox时,它的工作情况非常好。有了这个特定的控件,它会抛出一个xaml解析异常:“找不到名为'MetroCheckBox'的资源。资源名称区分大小写。”

我在源代码中的样子,试图追查这个问题,并直接从GitHub的coppied名称:

https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Controls.CheckBox.xaml

它肯定和我所有的其他控制工作的优良:

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MetroButton}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource MetroComboBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MetroCheckBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

有关如何解决此问题的任何想法?

注意,我包括参考样式,像这样:

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:extra="http://schemas.extra.com/ui" 
xmlns:system="clr-namespace:System;assembly=mscorlib" 
xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
xmlns:Primitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
xmlns:Custom="http://metro.mahapps.com/winfx/xaml/controls" 
x:Class="MyApp.App" 
StartupUri="/MyApp;component/GUI/Window/Window3.xaml" 
> 

<Application.Resources>  
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 

     <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MetroButton}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource MetroComboBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <!--<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MetroCheckBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style>--> 

     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

     <!--Chart Style--> 
     <Style TargetType="{x:Type chartingToolkit:Chart} 
    ... 
     </Style> 

     <!--Top Tab Item--> 
     <Style x:Key="TopTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}"> 
      ... 
     </Style> 

     <!--Tab Item--> 
     <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource MetroTabItem}"> 
    ... 
     </Style> 

     <!-- Group Box--> 
     <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource MetroGroupBox}"> 
      <!--<Setter Property="Margin" Value="5"/> 
      <Setter Property="Padding" Value="5"/> 
      <Setter Property="Foreground" Value="{DynamicResource BlackBrush}"/> 
      <Setter Property="Background" Value="{DynamicResource AccentColorBrush}"/> 
      <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/> 
      <Setter Property="Custom:ControlsHelper.HeaderFontSize" Value="{DynamicResource ContentFontSize}"/> 
      <Setter Property="Custom:GroupBoxHelper.HeaderForeground" Value="{x:Null}"/>--> 
      <Setter Property="Custom:ControlsHelper.HeaderFontWeight" Value="SemiBold"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type GroupBox}"> 
         <Grid x:Name="GroupBoxRoot"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,0,2" Background="Transparent" Grid.Row="0"> 
           <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" TextElement.FontWeight="{TemplateBinding Custom:ControlsHelper.HeaderFontWeight}" TextElement.FontStretch="{TemplateBinding Custom:ControlsHelper.HeaderFontStretch}" TextElement.FontSize="{TemplateBinding Custom:ControlsHelper.HeaderFontSize}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"/> 
          </Border> 
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" Background="Transparent" Grid.Row="1"> 
           <ContentPresenter Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ResourceDictionary> 
</Application.Resources> 

+1

您是否将MahApps ResourceDictionaries添加到您的应用程序或窗口资源中? – 2015-02-11 14:17:57

+0

是的,他们在Application.xaml中高于此值。 CheckBoxes(以及所有其他控件)在我运行时没有使用样式。它只有在我尝试使用BasedOn时才会出现此错误。 – Joe 2015-02-11 15:27:03

+1

@Joe我不能用v1.0.0重现这个,所以你用什么版本? – punker76 2015-02-11 22:10:58

回答

1

,它工作正常:

 <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource MetroComboBox}"> 
      <Setter Property="Margin" Value="2"/> 
     </Style> 

据我所知,我已经改变了与该项目或引用无关。但是,在过去的几天里,Visual Studio以某种方式破坏了设计者抛出异常左右中心,解决方案资源管理器无法显示任何东西(在清除组件模型缓存后已修复),并且NuGet无法为我检查MahApps版本。

因此,我在修复安装上启动了安装程序,现在样式工作正常。

我不知道这些是否连接...但他们可能是,所以我会留下这个答案,以防其他人遇到同样的问题。

1

您缺少所需的ResourceDictionary在样式的MahApps带来。您可以通过将ResourceDictionary定义包含在您编写视图的XAML文件的XAML中,或者将其添加到APP.XAML文件中 - 稍后您可以在短期内为您提供更好的性能。

例如:这里是MahApps控制包括与其他MahApps资源字典XAML,控件XAML是你在这种情况下,需要一个:我今天跑了

<Application x:Class="MyApp" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Application.Resources> 

     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 

       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> 

      </ResourceDictionary.MergedDictionaries> 

     </ResourceDictionary>  

    </Application.Resources> 
</Application> 
+0

对不起,我没有包括整个应用程序代码。我在目录区域中引用所有这些。我会修改这个问题 – Joe 2015-02-11 15:23:05