如何从文件添加图标到MahApps.Metro WPF窗口?

时间:2015-12-18 18:40:39

标签: c# wpf xaml mahapps.metro

MahApps.Metro的documentation在其工具栏的左上角有一个图标。它被称为Window Icon,我一直在努力工作。

我有一个名为image.ico的文件,我已将其作为资源添加到VisualStudio 2013中,转到Project -> myproject Properties... -> Resources Tab -> Add Existing File... -> Selecting image

该文件现在被列为名为“image”的资源,其持久性设置为“在编译时链接”。

我尝试了两种不同的策略来实现这一目标。第一个是设置IconShowIconOnTitleBar选项。

方法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选项的错误。我相信我要么设置资源不正确,要么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>

这给我错误'资源'图像“无法解析”。

感谢任何帮助。

0 个答案:

没有答案