问题
MahApps.Metro's documentation has an icon at the top left of their tool bar. It's called a Window Icon, and I've been having trouble getting one working.
I have a file called image.ico and I've added it as a resource in VisualStudio 2013, by going to Project -> myproject Properties... -> Resources Tab -> Add Existing File... -> Selecting image
The file is now listed as a resource named 'image', with its persistence set to 'Linked at compile time'.
I've tried two different tactics to get this to work. The first one, was setting Icon
and ShowIconOnTitleBar
options.
Method1
<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">
This gives me an error for the Icon
option. I believe I am either setting the Resource incorrectly, or Icon wants something else entirely.
The second method is to still have the ShowIconOnTitleBar="True"
, but set everything else in an IconTemplate
.
Method2
<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>
This gives me the error 'The Resource "image" could not be resolved'.
Any help is appreciated.
回答1:
Right-click your project, Add -> Existing Item... and then select the desired ico
file.
In your XAML assign the ico
file name to the Icon
property as below:
<Controls:MetroWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Icon="mahapps.metro.logo2.ico"
Background="LightGray"
Title="My Demo MetroWindow With Icon" Height="350" Width="525">
<Grid>
</Grid>
来源:https://stackoverflow.com/questions/34362017/how-to-add-an-icon-from-file-to-mahapps-metro-wpf-window