【发布时间】:2012-11-16 21:24:37
【问题描述】:
我正在开发一个 WPF 应用程序,并尝试在运行时更改颜色。我尝试了这个帖子中发布的解决方案:WPF: Changing Resources (colors) from the App.xaml during runtime;但是,该解决方案似乎不起作用。
我定义了 3 个资源字典
Colors1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#ffaa01</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
Colors2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#aaff01</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
Colors3.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BaseColor">#aa01ff</Color>
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" />
</ResourceDictionary>
所有 3 个资源字典都位于我的应用程序根目录中名为“Resources”的子文件夹中。
我已将 Colors1 资源字典添加到应用程序资源(在 Application.xaml 文件中),以便默认加载。
这是我的 Application.xaml:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="Colors1" Source="Resources/Colors1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我的应用程序中有一个名为“Window1”的窗口,其中包含一个网格和一个允许您在资源字典之间切换的组合框。
这是 Window1 的 xaml 代码:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Picture Orders" Height="600" Width="600"
xmlns:myProj="clr-namespace:TryingWPF">
<Window.Resources>
<x:Array x:Key="ResourceNames" Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>Colors1</sys:String>
<sys:String>Colors2</sys:String>
<sys:String>Colors3</sys:String>
</x:Array>
</Window.Resources>
<Grid x:Name="VisualRoot" Background="{DynamicResource BackgroundColorBrush}">
<ComboBox x:Name="ResourceOptions"
ItemsSource="{StaticResource ResourceNames}"
SelectedIndex="0"
VerticalAlignment="Top"
HorizontalAlignment="Center"
SelectionChanged="ResourceOptions_SelectionChanged"/>
</Grid>
</Window>
这里是处理 ComboBox 的 Selection Changed 事件的 VB.NET 代码,该事件应该改变窗口中 Grid 的背景颜色:
Public Class Window1
Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue)
Dim newResourceDictionary As New ResourceDictionary()
newResourceDictionary.Source = New Uri(resourceSource)
Application.Current.Resources.MergedDictionaries.RemoveAt(0)
Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary)
End Sub
End Class
我不确定自己做错了什么。
如何改变颜色?
*编辑:* 好吧,刚发完这个问题,我发现如果我把画笔从颜色资源文件中移到Window资源中,切换资源时颜色会发生变化。
但是,如果我将画笔移动到另一个名为 ColorBrushes 的 ResourceDictionary,并简单地切换 Color ResourceDictionaries,它不会改变颜色。
在我的主应用程序中,我所有的画笔都在 ResourceDictionaries 中定义,而不是在窗口或用户控件本身中定义......所以这对我来说仍然是个问题。
【问题讨论】:
-
我做了类似的事情来处理我的应用程序中的几种语言,它可以工作......我看到的唯一区别是我在删除旧资源字典之前添加了新资源字典,因为我没有不希望资源在短时间内未定义。 (以防引发异常等)。值得尝试。另一个想法:在更改字典之前/之后使用调试并注意资源的值(Me.TryFindResource(key))。