Search code examples
c#.netavaloniauiavaloniamergeddictionaries

Adding ResourceDictionary to Application.Current.Resources.MergedDictionaries (Avalonia UI)


I want to add resources through code, but I can't do it.

In WPF, I would have done that:

Application.Current.Resources.Clear();
Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(
    new Uri(@"Resources\Style\light.xaml", UriKind.Relative)) as ResourceDictionary);

But Avalonia doesn't have an Application.LoadComponent.

I try to do it, but it doesn't work:

Application.Current?.Resources.MergedDictionaries.Clear();
Application.Current?.Resources.MergedDictionaries.Add( new ResourceInclude
{
    Source = new Uri(@"/Assets/icons/LightIcons.axaml", UriKind.Relative)
});

If you know how to do this, please help. Thank you in advance.


Solution

  • I found the answer, it's the right thing to do:

    Application.Current?.Resources.MergedDictionaries.Clear();
    Application.Current?.Resources.MergedDictionaries.Add(new ResourceInclude
        {
            Source = new Uri("avares://MyApplication/Assets/icons/DarkIcons.axaml")
        });