我想要什么
我想在多个UserControl类型之间重用一些样式。
我想要一些Border控件的背景闪存,我希望它们都使用相同的风格,静态资源和动画,以便它们都同步闪烁。
我是怎么做的
为此,我在资源字典中定义了一些常见的颜色,如下所示:
<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />...and我还在本词典中定义了一个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>然后,在其他各种作为...and子UserControl的UserControl中,我重新导入ResourceDictionary,并将{StaticResource StatusErrorBackground}用于Background。
所讨论的元素是红色的(如SolidColorBrush声明中的那样),但它们没有闪烁。
迄今为止的模糊理解
也许这样做不会对所讨论的元素引发适当的PropertyChanged通知,因此它们不会被重绘?或者类似的东西。Color属性在SolidColorBrush上并不是一个依赖属性,但是SolidColorBrush实现了IAnimatable,所以在这里我不理解的幕后显然有魔力发生。
还是因为我在两个不同的地方导入相同的资源字典(一次在我的顶级UserControl中,另一次在我的孩子中),所以我最终得到了两个独立的StaticResource引用?如果您在两个不同的控件中导入相同的ResourceDictionary文件,它是否为每个控件创建独立的资源?在这种情况下,我可以通过在应用程序的层次上来解决这个问题,我想……
有人能告诉我我做错了什么,我该怎么做才能修复它吗?
发布于 2012-08-10 18:06:06
我真的不能解释为什么,但是如果您添加一个虚拟对象(例如,一个使用SolidColorBrush作为其Fill属性的矩形),然后将该Fill动画化,它就能工作。现在Solid色刷的颜色变得动画片。
<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />
<Rectangle x:Key="StatusErrorBackgroundRectangle" Fill="{StaticResource StatusErrorBackground}"/>
<Storyboard x:Key="BackgroundAnimation">
<ColorAnimation
Storyboard.Target="{DynamicResource StatusErrorBackgroundRectangle}"
Storyboard.TargetProperty="Fill.(SolidColorBrush.Color)"
... />
</Storyboard>发布于 2015-10-05 12:38:28
我知道我来这里晚了,但我发现这个问题时,我试图动画的颜色,是用实心的画笔。刷子是其他人正在使用的资源字典的一部分,我不想更改。我刚做了这个,很快就试了一下,看起来很管用。
public class SolidColorAnimation : ColorAnimation
{
public SolidColorBrush ToBrush
{
get { return To == null ? null : new SolidColorBrush(To.Value); }
set { To = value?.Color; }
}
}并在xaml中这样使用:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard >
<ui:SolidColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" ToBrush="{StaticResource NavButtonRolloverBrush}" Duration="0:0:0.75"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard >
<ui:SolidColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" ToBrush="{StaticResource NavButtonBrush}" Duration="0:0:0.25"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>发布于 2020-12-13 18:52:22
只管用
<Color x:Key="name" A="0" R="255" G="255" B="255"/>
而不是
<SolidColorBrush x:Key="name" Color="#0fff"/>
对我来说很管用
https://stackoverflow.com/questions/11906335
复制相似问题