简体   繁体   English

使用静态资源设置窗口图标

[英]Setting window icon with a static resource

I am trying to set the window icon in a WPF application that uses MahApps.Metro. 我试图在使用MahApps.Metro的WPF应用程序中设置窗口图标。 I don't know how to set a static resource that I defined after the window's XAML to be the window's icon. 我不知道如何将在窗口的XAML之后定义的静态资源设置为窗口的图标。

Below is the XAML, the resource I want is in Icons.xaml : 下面是XAML,我想要的资源在Icons.xaml

<controls:MetroWindow x:Class="Test1.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="MainWindow" Height="350" Width="525" WindowState="Maximized">

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.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="/Resources/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Here is an example of how I use the resource on the window: 这是我如何在窗口使用资源的示例:

<Rectangle>
    <Rectangle.Fill>
        <VisualBrush Visual="{StaticResource appbar_alien}" />
    </Rectangle.Fill>
</Rectangle>

How do I set appbar_alien as the window's icon? 如何将appbar_alien设置为窗口的图标?

I've just answered to a question about icon too big in MahApps MetroWindow ( Application icon stretches to title bar height when using MahApps.Metro ) and same technique could give you a solution. 我刚刚回答了有关MahApps MetroWindow中的图标太大的问题( 使用MahApps.Metro时,应用程序图标会延伸到标题栏的高度 ),并且相同的技术可以为您提供解决方案。

Basically, you have to set MetroWindow.IconTemplate property: 基本上,您必须设置MetroWindow.IconTemplate属性:

<MahApps:MetroWindow.IconTemplate>
    <DataTemplate>
        <Grid Width="{TemplateBinding Width}"
                 Height="{TemplateBinding Height}"
                 Margin="4"
                 Background="Transparent"
                 RenderOptions.EdgeMode="Aliased"
                 RenderOptions.BitmapScalingMode="HighQuality">
                 <Rectangle>
                     <Rectangle.Fill>
                         <VisualBrush Visual="{StaticResource appbar_alien}" />
                     </Rectangle.Fill>
                 </Rectangle>
        </Grid>
    </DataTemplate>
</MahApps:MetroWindow.IconTemplate>

Depending of your "/Resources/Icons.xaml", you may have to stretch/scale it. 根据您的“ /Resources/Icons.xaml”,您可能不得不拉伸/缩放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM