1

In my WPF User Control, I have added an icon (shown in the XAML below). But at run time, the InitializeComponent() call in the following code of the user control gives the error shown below:

UserControl1.xaml.cs

public partial class UserControl1 : System.Windows.Window
    {
        public UserControl1()
        {
            InitializeComponent();
        }
…………….
}

XAML of the User Control (with icon):

<Window x:Class="MyProject.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyProject"
             mc:Ignorable="d"
            "d:DesignHeight="450" d:DesignWidth="800"
             Icon="pack://siteoforigin:,,,/Resources/test_32.png">
    <Grid>
………………
    </Grid>

Error:

DirectoryNotFoundException: Could not find a part of the path 'C:\MyFolder\VSTO\MyProject\bin\Debug\Resources\MyIcon.png'.

Remark:

I noticed that Resources folder does not get created under Debug folder although it is part of the project. I have set MyIcon.png file with Build action as Resource and Copy to output directory is set to Copy if newer

nam
  • 21,967
  • 37
  • 158
  • 332

1 Answers1

1

I don't why but you should use application with resources Uri and siteoforigin with the rest.

You can either change your Uri to:

Icon="pack://application:,,,/Resources/test_32.png"

Or if you want to keep using siteoforigin than change the build option to "content" instead of "Resource". Related topics:

What is application's site of origin and when to use it

pack-uris-in-wpf

Clemens
  • 123,504
  • 12
  • 155
  • 268
Cfun
  • 8,442
  • 4
  • 30
  • 62
  • Your suggestion worked (thank you). I changed the build action to `content`. Also, your references were of great help (it's a good practices so others can get help, as well) – nam May 23 '20 at 06:35