klop52
klop52

Reputation: 93

How to add icon to wpf Application in Visual Studio Code

I am trying to add file.ico to my WPF Application. I know that in Visual Studio it's easier, however I am confused about how to add it in Visual Studio Code. So Basically I want to add the file.ico in the project folder such that the app also works on other computers.

Note: it works when I add the full path in the MainWindow.xaml ( Icon="Path\file.ico" ), but instead I want to write just Icon="file.ico" (the file.ico can also be in a folder in the project). I know that I need to add resources in the App.xaml and Project.csproj , but I don't know how.

WPF is a relatively new topic for me and I would appreciate any help.

Thanks in advance

Upvotes: 5

Views: 7791

Answers (3)

Jim Broiles
Jim Broiles

Reputation: 413

I've seen a number of approaches to this problem that suggest using NotifyIcon from Windows.Forms to instantiate and show the icon. This overly complex. The good news is there is no need for this in WPF. It is extremely simple to add the application icon in WPF using Visual Studio.

Right click on the top level project folder in the solution explorer and go to properties.

The application properties window opens. enter image description here Click on Win32 Resources. Browse for the icon file in your project. Images of type .png or .jpg won't work. Google "png to ico" and you can get the ico file for free. Close the application properties window. Your done.

You will now see the icon in the System Tray and in the upper left corner of each window (if you use a variation of the default window style).

Upvotes: 3

klop52
klop52

Reputation: 93

I have found a solution, which is pretty simple: add this code to .csproj file

<ItemGroup>
    <Folder Include="Assets\" />
    <Resource Include="Assets/file.ico" />
</ItemGroup>

and then added this code to MainWindow.xaml file: Icon="/Assets/file.ico"

Upvotes: 3

Bryce Norton
Bryce Norton

Reputation: 11

Right click on your solution and click: Add > New folder. You can call it whatever you want for example 'Assets'. Right click on that folder and click add existing item. Make sure to change the file type to All files. Navigate over to your icon file and double click it. When it's imported click on it and make sure Build action is set to 'Content'. You can also use .png images for your icon

Upvotes: 1

Related Questions