将图标属性添加到 xaml



>我正在尝试向 wpf 窗口添加一个图标,但是每当我将以下行添加到代码中时,我都会收到 xaml 解析异常:

Icon="myIcon.ico"

我的窗口标签看起来像这样(并且运行良好(,没有 Icon 属性:

<Window x:Class="MyProject.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:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204">

如果我在>之前添加Icon="myIcon.ico",我会在Width="1058.204"W上出现错误

抛出的异常:"System.Windows.Markup.XamlParseException" in 演示框架.dll

附加信息:"提供价值 "System.Windows.Baml2006.TypeConverterMarkupExtension"抛出了一个 例外。行号"8"和行位置"58"。

因此,错误代码如下所示:

<Window x:Class="MyProject.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:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="myIcon.ico">

我觉得我在这里一定错过了一些非常简单的东西。基于此处的其他帖子(如何在WPF窗口中更改标题栏图像?我觉得我做对了。

谁能帮忙?谢谢!

我在 m.rogalski 的建议和使用此处的信息的帮助下解决了这个问题:如何在 XAML 中引用图像资源?

将图像导入项目后,我将代码更改为如下所示,并且它起作用了:

<Window x:Class="MyProject.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:local="clr-namespace:MyProject"
mc:Ignorable="d"
Title="My Project" Height="562.356" Width="1058.204"
Icon="pack://application:,,,/my folder/myIcon.ico">

如果我在>之前添加Icon="myIcon.ico",我在Width="1058.204"W上出现错误

myIcon.ico文件添加到项目中,并在 Visual Studio 的属性窗格中将其"生成操作"属性设置为"资源"。

然后,可以将窗口的 Icon 属性设置为相对 URI 或包 URI,也可以在 Project->属性->应用程序-> 图标和清单下将该图标指定为应用程序的默认图标。

最新更新