【发布时间】:2015-12-18 18:40:39
【问题描述】:
MahApps.Metro 的 documentation 在其工具栏的左上方有一个图标。它称为Window Icon,但我一直无法让其工作。
我有一个名为 image.ico 的文件,我已将其添加为 VisualStudio 2013 中的资源,方法是转到 Project -> myproject Properties... -> Resources Tab -> Add Existing File... -> Selecting image
该文件现在列为名为“图像”的资源,其持久性设置为“编译时链接”。
我尝试了两种不同的策略来让它发挥作用。第一个是设置Icon 和ShowIconOnTitleBar 选项。
方法1
<Controls:MetroWindow x:Class="myprogram.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="MyProgram" Height="400" Width="800"
BorderThickness="2"
BorderBrush="{DynamicResource AccentColorBrush}"
SaveWindowPosition="True"
Icon="{StaticResource image}"
ShowIconOnTitleBar="True">
这给了我Icon 选项的错误。我相信我要么错误地设置了资源,要么图标完全想要别的东西。
第二种方法是保留ShowIconOnTitleBar="True",但将其他所有内容设置为IconTemplate。
方法2
<Controls:MetroWindow x:Class="myprogram.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="MyProgram" Height="400" Width="800"
BorderThickness="2"
BorderBrush="{DynamicResource AccentColorBrush}"
SaveWindowPosition="True"
ShowIconOnTitleBar="True">
<Controls:MetroWindow.IconTemplate>
<DataTemplate>
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="4"
Background="Transparent"
RenderOptions.EdgeMode="Aliased"
RenderOptions.BitmapScalingMode="HighQuality">
<Image Source="{StaticResource image}"></Image>
</Grid>
</DataTemplate>
</Controls:MetroWindow.IconTemplate>
这给了我错误“无法解析资源“图像””。
感谢任何帮助。
【问题讨论】:
-
在您的 XAML 中,只需创建
Icon="your_icon_file_name.ico",其中your_icon_file_name.ico将作为现有项目添加到您的项目中。 -
当您说“作为现有项目添加到项目中”时,您的意思是在资源下添加吗?如果是这样,那么当我有
Icon="image.ico"时,它会说“找不到 C:\Program Files (x86)\Microsoft Visual Studio 12\Common7\IDE\image.ico” -
右键单击您的项目并添加 -> 现有项目...,然后选择您的 ico 文件。
-
它对你有用吗?
-
它确实有效。设置为答案,我会接受。谢谢
标签: c# wpf xaml mahapps.metro