ChrisUK
ChrisUK

Reputation: 687

How to remove App.xaml ResourceDictionary at startup?

I have a ResourceDictionary declared in the App.xaml file as below:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/DefaultSkin.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The problem is that when I attempt to load a different skin at start-up (using the App.xaml.cs constructor to load the last used ResourceDictionary skin) I find that the ResourceDictionary set in Application.Resources overrides this and reverts back to the DefaultSkin.xaml file - even when I use Application.Current.Resources.MergedDictionaries.Clear(); before choosing the required skin.

My app works perfectly when I remove the ResourceDictionary from Application.Resources - but then all xaml references are lost at design time. How can I keep this reference at design time but remove it at runtime before it can override my skin choice?

Upvotes: 3

Views: 961

Answers (1)

mm8
mm8

Reputation: 169300

Override the OnStartup method in App.xaml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Resources.MergedDictionaries.Clear();
        //...
    }
}

If you want to clear MergedDictionaries in the constructor, you should do it after you have called InitializeComponent():

public App()
{
    InitializeComponent();
    Resources.MergedDictionaries.Clear();
}

Upvotes: 5

Related Questions