【发布时间】:2012-01-25 06:47:30
【问题描述】:
现在我的应用中有一些隐式样式的 TabItem。我想在我的应用中添加“夜间模式”并改变我的风格。我该怎么办?
【问题讨论】:
现在我的应用中有一些隐式样式的 TabItem。我想在我的应用中添加“夜间模式”并改变我的风格。我该怎么办?
【问题讨论】:
您可以使用合并的字典来做到这一点。将所有“正常”样式放入字典中,并默认将其添加到应用资源中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Normal.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
然后你可以删除当前字典并动态加载另一个:
private void ChangeStyles()
{
App.Current.Resources.MergedDictionaries.Clear();
StreamResourceInfo resInfo = App.GetResourceStream(new Uri("Styles/NewStyles.xaml", UriKind.Relative));
XDocument xaml = XDocument.Load(resInfo.Stream);
ResourceDictionary resource = XamlReader.Load(xaml.ToString()) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resource);
}
【讨论】:
阿方索的想法是对的…… 但你必须在 WPF 中这样做
App.Current.Resources.MergedDictionaries.Clear();
Uri uri = new Uri("/Resources/GlassButton5Night.xaml", UriKind.Relative);
var resDict = Application.LoadComponent(uri) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resDict);
并且您已确保在正确的级别重置您的 MergedDictionaries
【讨论】: