How can I merge two ResourceDictionary objects into Application.Current.Resources using C# instead of XAML?

261 views Asked by At

Here is the situation that I have. Two resource dictionaries labeled for this example Theme1 and Theme2

public class Theme1 : ResourceDictionary
{
    protected Theme1()
    {
        Add(nameof(IconColor), "#111111");
        Add(nameof(PageBackgroundColor), "#111111");
    }
    public Color IconColor { get; }
    public Color PageBackgroundColor { get; 
}

public class Theme2 : ResourceDictionary
{
    protected Theme2()
    {
        Add(nameof(IconColorA), "#222222");
        Add(nameof(PageBackgroundColorA), "#222222");
    }
    public Color IconColorA { get; }
    public Color PageBackgroundColorA { get; }
}

I would like to assign both of these to Application.Current.Resources but I only know how to do one or the other:

Application.Current.Resources = new Theme1();

or

Application.Current.Resources = new Theme2();

How can I assign both to Application.Current.Resources? I think there is a way to do this in XAML but I cannot find any way to do this in C#.

1

There are 1 answers

0
Cfun On BEST ANSWER

You can add them to MergedDictionaries property:

Application.Current.Resources.MergedDictionaries.Add(new Theme1());
Application.Current.Resources.MergedDictionaries.Add(new Theme2());