MahApps RightWindowCommand中没有出现图标

时间:2016-10-15 19:40:49

标签: c# wpf xaml icons mahapps.metro

我几天前开始使用mahapps(仍然在学习),我尝试在标题栏中添加一个图标,我在网站上做了与示例相同的一切,但它不会出现。这就是我正在尝试的事情:

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
    <StackPanel Orientation="Horizontal">
        <Rectangle Width="20"
               Height="20"
               Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}" />
            </Rectangle.OpacityMask>
        </Rectangle>
        <Button Margin="4 0 0 0" Content="settings" Click="Button_Click"/>
    </StackPanel>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>`

我的app.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/Controls.AnimatedTabControl.xaml" />
            <ResourceDictionary Source="/Resources/Icons.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>

如何显示图标?以下是我希望它的示例:What it should look like

2 个答案:

答案 0 :(得分:1)

试试这个:

<强> MainWindow.xaml:

<Controls:MetroWindow x:Class="MahApps.Metro.Application31.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  Icon="mahapps.metro.logo2.png"
                  BorderBrush="{StaticResource AccentColorBrush}"
                  BorderThickness="2"
                  Title="MainWindow"
                  Height="350"
                  Width="350">

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
        <Button>
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="20"
                           Height="20"
                           Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                    <Rectangle.OpacityMask>
                        <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}" />
                    </Rectangle.OpacityMask>
                </Rectangle>
                <TextBlock Margin="5,0,0,0">Settings</TextBlock>
            </StackPanel>
        </Button>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>

<Grid>
    <Rectangle Fill="{StaticResource AccentColorBrush}">
        <Rectangle.OpacityMask>
            <VisualBrush Stretch="Fill"
                         Visual="{StaticResource appbar_cog}" />
        </Rectangle.OpacityMask>
    </Rectangle>
</Grid>

<强> App.xaml中:

<Application x:Class="MahApps.Metro.Application31.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.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 Source="pack://application:,,,/MahApps.Metro.Resources;component/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

enter image description here

注意:要使一切正常工作,您需要:

1)明确地将mahapps.metro.logo2.png添加到您的项目中; 2)明确地向MahApps.Metro.Resources

添加项目引用

enter image description here

答案 1 :(得分:0)

在代码中使用IconShowIconOnTitleBar以及ShowTitleBar,如下所示:

<Controls:MetroWindow x:Class="MetroDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Title="My Application"
        Width="960" Height="600"
        Icon="mahapps.metro.logo2.ico"
        ShowIconOnTitleBar="True"
        ShowTitleBar="True">
    <Grid />
</Controls:MetroWindow>

修改 Demo App