【问题标题】:.NET - ColorAnimation doesn't work.NET - ColorAnimation 不起作用
【发布时间】:2010-09-22 07:54:31
【问题描述】:

我为 SpotLight 对象创建了 ColorAnimation,但它似乎不起作用。 我做错了什么?

ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
mouseEnterColorAnimation.To = Colors.Red;
mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(mouseEnterColorAnimation, "MyAnimatedBrush");

Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.Color));
Storyboard storyboard = new Storyboard();
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.Children.Add(mouseEnterColorAnimation);
storyboard.Begin(this);

【问题讨论】:

    标签: c# .net visual-studio coloranimation


    【解决方案1】:

    使用Storyboard.SetTargetName 时,名称必须是您要为属性设置动画的FrameworkElement 实例的实际Name 属性的值。因此,在您的情况下,可能是 SpotLightAuditorium 控件的一个实例:

    Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);
    

    propertypath 必须是对实际依赖属性的引用:

    Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.ColorProperty));
    

    如果您想直接为画笔设置动画(至于没有 Name 属性),您必须使用 RegisterName 在当前 Page/UserControl/Window 中注册画笔的名称,这与使用 XAML @987654328 相同@。

    或者,您可以对派生自 Animatable 的元素使用以下方法:

    ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
    mouseEnterColorAnimation.To = Colors.Red;
    mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
    myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, null); // remove the old animation to prevent memoryleak
    myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, mouseEnterColorAnimation);
    

    【讨论】:

    • SpotlightAuditorium 是 Spotlight 的一个具体实例,它似乎没有名称属性。另外,我不明白您对 SpotLightAuditorium.ColorProperty 的意思。
    • 我假设您的 SpotLightAuditorium 将获得一个 Color 依赖属性,然后您将使用它来制作动画。如果 Spotlight 没有 name 属性,则必须使用我的答案中描述的 RegisterName 方法来设置名称。
    • 如何设置颜色依赖属性?我也不明白为什么我需要刷子而不是常规颜色(因为 SpotLight(3D-Light))只有颜色属性。抱歉问了这么多。
    • 啊 - 我们在这里谈论 System.Windows.Media.Media3D.SpotLight。这个类有一个 Color 依赖属性。您可以将它用于动画(没有 Brush invloved-"MyAnimatedBrush" 让我假设您可能在某处有刷子。因此您可以调用 RegsiterName 来获取 Spotlight,然后调用 Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name),然后调用 @987654332 @
    • 耶!它终于奏效了。为什么 SetTarget 而不是 SetTargetName 不起作用?
    【解决方案2】:

    您没有在页面上注册画笔的名称,以便故事板可以定位它:

    SolidColorBrush myAnimatedBrush = new SolidColorBrush();
    myAnimatedBrush.Color = ?? choose a color
    
    this.RegisterName("MyAnimatedBrush", myAnimatedBrush);
    

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 2016-10-11
      • 2016-08-22
      • 1970-01-01
      • 2019-01-22
      • 2017-06-14
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多