【发布时间】:2012-08-10 17:14:01
【问题描述】:
我想要什么
我想在多个UserControl 类型中重复使用一些样式。
我希望一些Border 控件的背景闪烁,并且我希望它们都使用相同的样式、静态资源和动画,以便它们都同步闪烁。
我是如何做到的
为此,我在资源字典中定义了一些常用颜色,如下所示:
<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />
...我还在这本词典中定义了一个 StoryBoard,如下所示:
<Storyboard x:Key="BackgroundAnimation">
<ColorAnimation
Storyboard.Target="{StaticResource StatusErrorBackground}"
Storyboard.TargetProperty="Color"
From="#440000"
To="#ff0000"
Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="True"/>
</Storyboard>
然后我将以下内容添加到顶级UserControl:
<FrameworkElement.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</FrameworkElement.Resources>
<FrameworkElement.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource BackgroundAnimation}"/>
</EventTrigger>
</FrameworkElement.Triggers>
...然后在其他各种UserControls 中,我重新导入上面的ResourceDictionary 并使用{StaticResource StatusErrorBackground} 作为Background。
有问题的元素是红色的(就像在SolidColorBrush 声明中一样),但它们没有闪烁。
到目前为止的模糊理解
也许这样做不会针对相关元素引发适当的 PropertyChanged 通知,因此它们不会被重绘?或类似的东西。 SolidColorBrush 上的 Color 属性不是依赖属性,但SolidColorBrush 实现了IAnimatable,所以这里显然是在幕后发生的魔术,我不明白。
或者是因为我在两个不同的地方(一次在我的顶级UserControl 加上一次在我的孩子)中导入了相同的资源字典,我最终得到了两个独立的StaticResource 引用?如果您在两个不同的控件中导入相同的ResourceDictionary 文件,是否会为每个控件创建独立的资源?在这种情况下,我想我可以通过将其拉入应用程序级别来解决此问题...
谁能告诉我我做错了什么以及如何解决它?
【问题讨论】:
标签: wpf animation resourcedictionary brush staticresource