尝试为SolidColorBrush静态资源设置动画

时间:2012-08-10 17:14:01

标签: wpf animation resourcedictionary brush staticresource

我想要什么

我想在多个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>

...然后在其他UserControl个孩子中,我重新导入ResourceDictionary,如上所述,并使用{StaticResource StatusErrorBackground}作为Background

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


到目前为止的模糊理解

也许这样做不会对相关元素引发相应的PropertyChanged通知,因此它们不会被重绘?或类似的东西。 Color上的SolidColorBrush属性不是依赖属性,但是SolidColorBrush实现了IAnimatable,所以我不明白这些幕后发生了明显的魔法。

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

任何人都可以告诉我我做错了什么以及如何修复它?

3 个答案:

答案 0 :(得分:5)

我知道我已经迟到了,但是当我试图用固体画笔中的颜色设置动画时,我发现了这个问题。画笔是其他人正在使用的资源字典的一部分,我不想改变。我刚刚做了这个,很快就尝试了,它似乎有效。

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>

答案 1 :(得分:1)

我不能真正说出原因,但是如果你添加一个虚拟对象,它就可以了。一个Rectangle,它使用SolidColorBrush作为其Fill属性,然后为Fill设置动画。现在SolidColorBrush的Color变得动画了。

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

答案 2 :(得分:0)

只需使用

<Color x:Key="name" A="0" R="255" G="255" B="255"/>

代替

<SolidColorBrush x:Key="name" Color="#0fff"/>

对我有用的

相关问题