MahApps 中的样式 "Cannot find resource 'MetroCheckBox'"

Style "Cannot find resource 'MetroCheckBox'" in MahApps

我开始在 WPF 中使用样式。我使用 MahApps 样式作为基础,到目前为止效果非常好。我已经能够使用 BasedOn 属性.

进行特定修改

其中一项更简单的更改是添加默认边距,以便在添加控件时它们不会接触。在我尝试使用 MetroCheckBox 之前,它一直运行良好。使用此特定控件,它会抛出一个 xaml 解析异常:"Cannot find resource named 'MetroCheckBox'. Resource names are case sensitive."

我查看了源代码,试图找出这个问题并直接从 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>

您缺少引入 MahApps 样式所需的 ResourceDictionary。您可以将 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>

我今天 运行 它工作正常:

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

据我所知,我所做的更改与项目或参考文献无关。

但是,在过去的几天里,Visual Studio 以某种方式损坏,设计人员在左右和中间抛出异常,解决方案资源管理器无法显示任何内容(已在清除组件模型缓存后修复)并且 NuGet 无法工作让我检查 MahApps 版本。

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

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

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

详情请见: mahapps.com:迁移到 v2.0 - MahApps.Metro https://mahapps.com/docs/guides/migration-to-v2.0