3

I'm trying to add an icon to a wpf window, but I'm getting an xaml parsing exception whenever I add the following line to my code:

Icon="myIcon.ico"

My window tag looks like this (and runs fine) without the Icon property:

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

If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '58'.

So, the code erroring out looks like this:

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

I feel like I must be missing something very simple here. Based off of other posts here (How to change title bar image in WPF Window?) I feel like I'm doing it right.

Can anyone help? Thanks!

Community
  • 1
  • 1
Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
  • 1
    First of all import your icon file into the project. Then rebuild your project and at last try to add it using `Icon="path\to\icon\folder\file.ico"`. – mrogal.ski Apr 12 '17 at 12:59
  • @m.rogalski I imported it to the project, and used the entire file path as you showed, and it worked. Do you know how I could reference this file with out "C:\foo\bar" ? Ultimately, I need the users who install this program to view the icon, and they can't with it referencing my local machine. – Joshua Schlichting Apr 12 '17 at 13:09
  • I figured it out. Icon="pack://application:,,,/my folder/myIcon.ico – Joshua Schlichting Apr 12 '17 at 13:15

2 Answers2

5

I solved this with a little bit of help from m.rogalski's suggestion and using information in here:How to reference image resources in XAML?

After importing my image to the project, I changed my code to look like this, and it worked:

<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">
Community
  • 1
  • 1
Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
3

If I add Icon="myIcon.ico" before the >, I get an error on the W in Width="1058.204"

Add the myIcon.ico file to your project and set its Build Action property to Resource in the properties pane in Visual Studio.

You can then either set the Icon property of the window to a relative URI or a pack URI or you could specify the the icon as the default icon of your application under Project->Properties->Application->Icon and manifest.

mm8
  • 163,881
  • 10
  • 57
  • 88