【问题标题】:Trying to animate a SolidColorBrush static resource尝试为 SolidColorBrush 静态资源设置动画
【发布时间】: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


    【解决方案1】:

    我知道我来晚了,但是当我尝试在实心画笔中保存的颜色进行动画处理时,我发现了这个问题。画笔是其他人正在使用的资源字典的一部分,我不想更改。我刚做了这个,很快就尝试了,它似乎有效。

    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>
    

    【讨论】:

      【解决方案2】:

      我真的不能说为什么,但如果你添加一个虚拟对象,它就会起作用,例如使用 SolidColorBrush 作为其Fill 属性的矩形,然后为Fill 设置动画。现在 SolidColorBrush 的颜色被动画化了。

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

      【讨论】:

      • 您的作品之所以有效,是因为您将 Rectangle 设置为 Storyboard.Target 的动画。他有多个目标并想要为它们设置动画。
      • 我试试看,谢谢。为了回答我自己的一些问题,当我从不同的 UserControls 执行此操作时,我肯定会得到两个独立的 StaticResource 引用(我猜这是有道理的),所以我首先需要将它拉到顶级资源中。会告诉你进展如何。
      • 这对我有用。我不知道为什么。直接为画笔设置动画似乎不起作用,这很烦人
      • 警告:如果您使用 XAML“类型转换器”设置画笔的颜色,这将不起作用,例如黄色
      【解决方案3】:

      随便用

      &lt;Color x:Key="name" A="0" R="255" G="255" B="255"/&gt;

      代替

      &lt;SolidColorBrush x:Key="name" Color="#0fff"/&gt;

      对我有用

      【讨论】:

        猜你喜欢
        • 2011-12-10
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2011-05-21
        • 2020-01-17
        • 2016-08-18
        • 2013-09-08
        • 2015-01-22
        相关资源
        最近更新 更多