如何在启动时删除 App.xaml ResourceDictionary? [英] How to remove App.xaml ResourceDictionary at startup?
问题描述
我在 App.xaml
文件中声明了一个 ResourceDictionary
,如下所示:
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>
问题是当我尝试在启动时加载不同的皮肤时(使用 App.xaml.cs
构造函数加载最后使用的 ResourceDictionary
皮肤)我发现 Application.Resources
中设置的 ResourceDictionary
覆盖了它并恢复到 DefaultSkin.xaml 文件 - 即使我使用 Application.Current.Resources.MergedDictionaries.Clear();
在选择需要的皮肤之前.
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.
当我从 Application.Resources
中删除 ResourceDictionary
时,我的应用程序运行良好 - 但是所有 xaml 引用在设计时都丢失了.如何在设计时保留此引用,但在运行时将其删除,然后才能覆盖我的皮肤选择?
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?
推荐答案
覆盖 App.xaml.cs
中的 OnStartup
方法:
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();
//...
}
}
如果你想在构造函数中清除MergedDictionaries
,你应该在调用InitializeComponent()
之后进行:
If you want to clear MergedDictionaries
in the constructor, you should do it after you have called InitializeComponent()
:
public App()
{
InitializeComponent();
Resources.MergedDictionaries.Clear();
}
这篇关于如何在启动时删除 App.xaml ResourceDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!