Setting window icon in Avalonia
Asked Answered
B

2

6

I am writing a small app for as a school project and I cannot figure out how can I change the window icon. I found the "Icon" property of Window, but I have no clue how it works, as I have found little documentation on it. When I tried to input something in the field, it threw an error, that the resource could not be found. I read something on importing resources as well, but this is my first app of this kind, so I am completely lost. Any help much appreciated, thank you

Bereft answered 29/4, 2021 at 21:21 Comment(0)
H
13

You need to add your icon as AvaloniaResource. If you are using MVVM template, everything in Assets directory should be added as one. If you aren't, then add

<ItemGroup>
  <AvaloniaResource Include="Assets\**" />
</ItemGroup>

to your .csproj file. Then put your icon into Assets directory. After that simply writing Icon="/Assets/your-icon.ico" in your window xaml should work.

Humbert answered 30/4, 2021 at 10:49 Comment(0)
W
6

This was more what I was looking for, in setting the icon in c#.

IBitmap bitmap = new Bitmap(AvaloniaLocator.Current?.GetService<IAssetLoader>()?.Open(
     new Uri($"avares://{Assembly.GetExecutingAssembly().GetName().Name}/Assets/example.png"))
);
var exampleWindow = new Window()
{
Title = "Example",
Height = 700,
Icon = new WindowIcon(bitmap)
}

in axaml

<Window xmlns="https://github.com/avaloniaui"
 Icon="/Assets/example.png"> 
...
</Window>
Waterford answered 26/9, 2022 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.