尝试制作SolidColorBrush静态资源的动画 [英] Trying to animate a SolidColorBrush static resource

查看:199
本文介绍了尝试制作SolidColorBrush静态资源的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个UserControl类型之间重用某些样式.

I want to reuse some styles across multiple UserControl types.

我希望某些Border控件的背景闪烁,并且我希望它们全部使用相同的样式,静态资源和动画,以便它们都同步闪烁.

I would like the background of some Border controls to flash, and I'd like them all to use the same style, static resource and animation, so that they all flash in sync.

为此,我在资源字典中定义了一些常见的颜色,如下所示:

To that end, I have defined some common colours in a resource dictionary, like so:

<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />

...而且我还在此字典中定义了一个StoryBoard,如下所示:

...and I have also defined a StoryBoard in this dictionary, like so:

<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:

I've then added the following to a top-level 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>

...然后在作为其子级的其他各种UserControl中,如上所述重新导入ResourceDictionary并将{StaticResource StatusErrorBackground}用作Background.

...and then in various other UserControls that are children of that, I re-import the ResourceDictionary as above and use the {StaticResource StatusErrorBackground} for a Background.

有问题的元素显示为红色(如SolidColorBrush声明中所示),但它们没有闪烁.

The elements in question are coloured red (as in the SolidColorBrush declaration), but they're not flashing.

也许这样做不会在有问题的元素上引发适当的PropertyChanged通知,因此它们不会重绘吗?或类似的东西. SolidColorBrush上的Color属性不是依赖项属性,但是SolidColorBrush实现了IAnimatable,因此显然在幕后发生了我不了解的魔术.

Maybe doing this doesn't raise the appropriate PropertyChanged notifications on the elements in question, so they're not redrawn? Or something like that. The Color property on SolidColorBrush isn't a dependency property, but SolidColorBrush implements IAnimatable, so there's obviously magic happening behind the scenes here I don't understand.

还是因为我在两个不同的位置(一次在我的顶级UserControl中,一次在我的孩子中)导入了相同的资源字典,所以我最终得到了两个独立的StaticResource引用?如果在两个不同的控件中导入相同的ResourceDictionary文件,它是否为每个控件创建独立的资源?我想在这种情况下,我可以通过在应用程序级别将其拉入来解决此问题.

Or is it that because I'm importing the same resource dictionary in two different places (once in my top-level UserControl plus once in my child) that I end up with two independent StaticResource references? If you import the same ResourceDictionary file in two different controls, does it create independent resources for each? In which case I might be able to fix this by pulling it in at an app level, I guess...

谁能告诉我我做错了什么以及如何解决?

Can anyone tell me what I doing wrong and how I might go about fixing it?

推荐答案

我无法真正说明原因,但是如果您添加了一个虚拟对象(例如,使用SolidColorBrush的Fill属性的矩形,然后为该Fill设置动画.现在,SolidColorBrush的颜色已设置动画.

I can't really say why, but it works if you add a dummy object, e.g. a Rectangle that uses the SolidColorBrush for its Fill property, and then animate that Fill. Now the SolidColorBrush's Color gets animated.

<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>

这篇关于尝试制作SolidColorBrush静态资源的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆