MahApps资源字典错误

时间:2017-04-23 15:32:48

标签: c# wpf mahapps.metro

所以,今天我在我的WPF项目中安装了MahApps。一切顺利,直到我想使用内置样式。

这是我的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"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <BitmapImage x:Key="logoImage" UriSource="Images/Logo.png" />
    <BitmapImage x:Key="profileButtonImage" UriSource="Images/ProfileButton.png" />
    <BitmapImage x:Key="settingsButtonImage" UriSource="Images/SettingsButton.png" />
    <BitmapImage x:Key="profileIconImage" UriSource="Images/profileIcon.png" />
    <BitmapImage x:Key="listenIconImage" UriSource="Images/listenIcon.png" />
    <BitmapImage x:Key="statsIconImage" UriSource="Images/statsIcon.png" />
    <BitmapImage x:Key="accountButtonImage" UriSource="Images/accountButton.png" />
    <BitmapImage x:Key="pauseButtomImage" UriSource="Images/pause-button.png" />
    <BitmapImage x:Key="playButtomImage" UriSource="Images/play-button.png" />
    <BitmapImage x:Key="stopButtomImage" UriSource="Images/stop-button.png" />
    <BitmapImage x:Key="doneButtonImage" UriSource="Images/doneButton.png" />
    <BitmapImage x:Key="arrowButtonImage" UriSource="Images/arrow.png" />
    <BitmapImage x:Key="doneButtonHoverImage" UriSource="Images/doneButtonHover.png" />
    <FontFamily x:Key="Novo">/Fonts/#Novecentosanswide-Medium</FontFamily>

    <Style x:Key="ButtonStyle">
        <Setter Property="Border.Background" Value="#262a33" />
        <Style.Triggers>
            <Trigger  Property="Border.IsMouseOver" Value="True">
                <Setter Property="Border.Background" Value="#1d2027" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <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" />
            <!-- Accent and AppTheme setting -->
            <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>

BitmapImages适用于我的一些按钮。我的问题是,当我尝试编译时,我收到以下错误:

All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
Each dictionary entry must have an associated key.

我安装了所有3个MahApps NuGets,你知道为什么会出现这些错误吗?

2 个答案:

答案 0 :(得分:0)

将所有资源包含在另一个ResourceDictionary内,并在其中添加MahApp资源MergedDictionaries

答案 1 :(得分:0)

感谢Ruben,为我工作只包装ResourceDictionary中的所有资源

 <Application.Resources>

    <ResourceDictionary>
        <!--Estilo Validação-->
    <Style TargetType="TextBox">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel>
                        <AdornedElementPlaceholder x:Name="placeholder"/>
                        <TextBlock FontSize="12" Foreground="Red"  
                                   Text="{Binding ElementName=placeholder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, 
                  Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!--FIM Estilo Validação-->

        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <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" />
            <!-- Accent and AppTheme setting -->
            <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>