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

Ala*_*Maw 3 wpf animation brush resourcedictionary staticresource

我想要什么

我想重用多种UserControl类型的一些样式.

我希望一些Border控件的背景能够闪现,我希望他们都能使用相同的样式,静态资源和动画,这样它们就会同步闪烁.


我是怎么做的

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

<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />
Run Code Online (Sandbox Code Playgroud)

...我还在这本词典中定义了一个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>
Run Code Online (Sandbox Code Playgroud)

然后我将以下内容添加到顶级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>
Run Code Online (Sandbox Code Playgroud)

...然后在其他各种UserControls中,我重新导入ResourceDictionary上面的内容并使用{StaticResource StatusErrorBackground}for a Background.

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


到目前为止模糊理解

也许这样做不会对有问题的元素引发相应的PropertyChanged通知,所以它们不会被重绘?或类似的东西.该Color物业SolidColorBrush是不是一个依赖项属性,但SolidColorBrush器物IAnimatable,所以很明显有神奇的事情发生在幕后这里后面我不明白.

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

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

小智 5

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

public class SolidColorAnimation : ColorAnimation
{
    public SolidColorBrush ToBrush
    {
        get { return To == null ? null : new SolidColorBrush(To.Value); }
        set { To = value?.Color; }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在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>
Run Code Online (Sandbox Code Playgroud)