Desfell
Desfell

Reputation: 79

Setting window icon in Avalonia

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

Upvotes: 7

Views: 7804

Answers (2)

lastlink
lastlink

Reputation: 1725

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>

Upvotes: 6

kekekeks
kekekeks

Reputation: 3623

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.

Upvotes: 14

Related Questions