【问题标题】:How To Add An Icon From File To MahApps.Metro WPF Window?如何将文件中的图标添加到 MahApps.Metro WPF 窗口?
【发布时间】: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

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

我尝试了两种不同的策略来让它发挥作用。第一个是设置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 选项的错误。我相信我要么错误地设置了资源,要么图标完全想要别的东西。

第二种方法是保留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


【解决方案1】:

右键单击您的项目,添加 -> 现有项...,然后选择所需的 ico 文件。

在您的 XAML 中,将 ico 文件名分配给 Icon 属性,如下所示:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多