将 Icon 属性添加到 xaml

Jos*_*ing 3 c# wpf xaml window

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

Icon="myIcon.ico"
Run Code Online (Sandbox Code Playgroud)

我的窗口标签看起来像这样(并且运行良好),没有 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">
Run Code Online (Sandbox Code Playgroud)

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

抛出异常:PresentationFramework.dll 中的“System.Windows.Markup.XamlParseException”

附加信息:“为‘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">
Run Code Online (Sandbox Code Playgroud)

我觉得我一定在这里错过了一些非常简单的东西。基于此处的其他帖子(How to change title bar image in WPF Window?),我觉得我做得对。

有人可以帮忙吗?谢谢!

Jos*_*ing 5

我在 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">
Run Code Online (Sandbox Code Playgroud)