【问题标题】:Icon Not Appearing In MahApps RightWindowCommandMahApps RightWindowCommand 中未出现图标
【发布时间】:2016-10-15 19:40:49
【问题描述】:

几天前我开始使用 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>

如何让图标出现?这是我希望它看起来像的示例:

【问题讨论】:

    标签: c# wpf xaml icons mahapps.metro


    【解决方案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>
    


    注意事项:要让一切正常工作,您需要:

    1) 将mahapps.metro.logo2.png 显式添加到您的项目中; 2) 显式添加对MahApps.Metro.Resources的项目引用

    【讨论】:

    • 谢谢!它起作用了,奇怪的是如何将它全部放在一个按钮中解决它
    【解决方案2】:

    在您的代码中使用IconShowIconOnTitleBarShowTitleBar,如下所示:

    <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

    【讨论】:

    • @ShakedDahan,我建议您下载 MahApps 演示应用程序(我在答案中添加了它)并逐行检查您的代码。还要检查正确路径中的图标文件。所以我想通过这种方式你可以很容易地找到问题并解决它。请给我反馈。
    • 我已经查过了,会不会是我的应用流向是 RightToLeft 的问题?
    • 不,我使用 MahApp 从右到左和从左到右。检查您的图标(.ico 文件)。请在您的问题中添加窗口Xaml 属性。
    • 我想你误会我说的那个图标了,我说的是这个:prntscr.com/curbm3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2015-03-31
    相关资源
    最近更新 更多