【问题标题】:Style "Cannot find resource 'MetroCheckBox'" in MahAppsMahApps 中的样式“找不到资源 'MetroCheckBox'”
【发布时间】:2015-04-11 21:51:09
【问题描述】:

我开始在 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>

【问题讨论】:

  • 您是否将 MahApps ResourceDictionaries 添加到您的应用程序或窗口资源中?
  • 是的,它们在 Application.xaml 的上面。 CheckBoxes(和所有其他控件)在我没有运行它时使用该样式很好。只有当我尝试使用 BasedOn 时,我才会收到此错误。
  • @Joe 我无法使用 v1.0.0 重现此问题,那么您使用什么版本?
  • @punker76。抱歉回复晚了,我的视觉工作室安装已损坏。我刚刚检查了我的 MahApps.Metro 安装(使用 NuGet),它被列为 1.0.0.0。编辑:由于某种原因重新安装它修复了错误...

标签: c# wpf xaml styles mahapps.metro


【解决方案1】:

您缺少引入 MahApp 样式所需的 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>

【讨论】:

  • 抱歉,我没有包含整个应用程序代码。我在目录区域中引用了所有这些。我会修改以将其包含在问题中
【解决方案2】:

我今天运行它,它运行良好:

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

据我所知,我没有更改与项目或引用有关的任何内容。

然而,在过去的几天里,Visual Studio 不知何故损坏了,设计师在左右和中间抛出异常,解决方案资源管理器无法显示任何内容(在清除组件模型缓存后已修复)并且 NuGet 无法让我检查MahApps 版本。

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多