wpf 使用静态资源设置窗口图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17508333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting window icon with a static resource
提问by Austin Henley
I am trying to set the window icon in a WPF application that uses MahApps.Metro. I don't know how to set a static resource that I defined after the window's XAML to be the window's icon.
我正在尝试在使用 MahApps.Metro 的 WPF 应用程序中设置窗口图标。我不知道如何将我在窗口的 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 onthe window:
这是我如何使用窗口上的资源的示例:
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_alien}" />
</Rectangle.Fill>
</Rectangle>
How do I set appbar_alienas the window's icon?
如何设置appbar_alien为窗口的图标?
回答by MuiBienCarlota
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”,您可能需要拉伸/缩放它。

