我目前正在尝试使用资源字典在我的应用程序中创建主题更改功能,这是我的XAML和VB代码,用于定义和设置此资源字典和其中一个字典:
窗口中的我的XAML我试图改变主题:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionaries/LightStyle.xaml">
</ResourceDictionary>
<ResourceDictionary Source="ResourceDictionaries/DarkStyle.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
...
<StackPanel Background="{DynamicResource AppBackgroundColour}"/>
我的VB改变了资源字典:
Private Sub MenuItemDarkTheme_Click(sender As Object, e As RoutedEventArgs)
resourceDictionaryUri = New Uri("ResourceDictionaries\DarkStyle.xaml", UriKind.Relative)
resourceDictionary = Application.LoadComponent(resourceDictionaryUri)
Application.Current.Resources.MergedDictionaries.Clear()
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary)
End Sub
这是我的两个资源词典之一,另一个是这样的,具有不同的颜色值:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Project_CodeShnippetz_Redesign">
<SolidColorBrush x:Key="AppBackgroundColour" Color="Black"/>
<SolidColorBrush x:Key="TabControlBackgroundColour" Color="Blue"/>
</ResourceDictionary>
以下是我点击其中一个菜单项时收到的错误消息:
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='Ũ'
我对我的问题进行了一些研究并找到了这个(Strange resource dictionary warnings appear in output window even when the WPF trace settings are turned off)与错误9相关的问题,这个答案表明这是一个微软没有修复的错误(至少他们没有在2012年修复过) -2014)因此我问是否有人有解决这个问题的方法?真正让我失望的是那个无法找到的奇怪的钥匙,我甚至没有提到那个&#34; U&#34;关键在哪里?我还读到其他人得到这个错误,但他们的资源库仍然有用。
非常感谢,任何帮助表示赞赏!
编辑:发现此博客文章(https://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored)也有一个解决方法,没有改变一件事。下面还提到过,我尝试使用StaticResource
代替DynamicResource
,但这似乎也无效。