问题
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?
回答1:
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();
}
来源:https://stackoverflow.com/questions/51420960/how-to-remove-app-xaml-resourcedictionary-at-startup