gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:57:31 25 4
gpt4 key购买 nike

我正在尝试在使用 MahApps.Metro 的 WPF 应用程序中设置窗口图标。我不知道如何将我在窗口的 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>

以下是我如何在窗口上使用资源的示例:

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

如何将 appbar_alien 设置为窗口图标?

最佳答案

我刚刚回答了有关 MahApps MetroWindow ( Application icon stretches to title bar height when using MahApps.Metro ) 中图标太大的问题,相同的技术可以为您提供解决方案。

基本上,您必须设置 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>

根据您的“/Resources/Icons.xaml”,您可能需要拉伸(stretch)/缩放它。

关于wpf - 使用静态资源设置窗口图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com