样式“在MahApps中找不到资源'MetroCheckBox'”

时间:2015-02-11 13:39:57

标签: c# wpf xaml styles mahapps.metro

我开始在WPF中使用样式。我使用MahApps风格作为基础,到目前为止一直非常好。我已经能够使用BasedOn属性进行特定修改。

更简单的更改之一是添加默认边距,因此在添加控件时,他们无法触摸。到目前为止,它一直工作得非常好,直到我尝试使用MetroCheckBox。使用此特定控件,它会抛出一个xaml解析异常:"找不到名为' MetroCheckBox'的资源。资源名称区分大小写。"

我看了一下源代码,试图找出这个问题并直接从GitHub中删除了这个名字:

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>

3 个答案:

答案 0 :(得分:2)

您缺少必需的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>

答案 1 :(得分:1)

我今天跑了它并且工作正常:

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

据我所知,我没有改变与项目或参考文件无关。

然而,在最近几天,Visual Studio以某种方式损坏了设计器抛出异常左右中心,解决方案资源管理器无法显示任何内容(在清除组件模型缓存后修复)和NuGet无法为我工作检查MahApps版本。

所以我在修复安装上启动了安装程序,现在样式运行正常。

我不知道这些是否相关......但它们可能是,所以我会把它留作答案,以防其他人遇到同样的问题。

答案 2 :(得分:0)

如果您从 2.0 之前的 MahApps 版本升级到更新的版本,则可能会导致这种情况。

欲知更多详情,请参阅: mahapps.com:迁移到 v2.0 - MahApps.Metro https://mahapps.com/docs/guides/migration-to-v2.0