简体   繁体   中英

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

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#.

You can add them to MergedDictionaries property:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM