本文介绍了Windows 8 - 在代码隐藏中动画自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想制作一堆形状并让它们动画化.所以我想出了以下自定义类:

Basically, I want to make bunch of Shapes and make them animated. So I came up with following custom class:

public class FunkyShape : DependencyObject
{
    public double Animator
    {
        get { return (double)GetValue(AnimatorProperty); }
        set { SetValue(AnimatorProperty, value); }
    }

    public static readonly DependencyProperty AnimatorProperty =
        DependencyProperty.Register("Animator", typeof(double), typeof(FunkyShape),
        new PropertyMetadata(0, new PropertyChangedCallback(Animator_Changed)));

    private static void Animator_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        double delta = (double)e.NewValue - (double)e.OldValue;

        ((FunkyShape)d).ProcessDelta((double)e.NewValue, delta);
    }

    private void ProcessDelta(double val, double delta)
    {
        Holder.Width = val;
        Holder.Height = val;

        // Keep shape centered
        HolderPosition.X = delta / 2;
        HolderPosition.Y = delta / 2;
    }

    private Shape Holder;
    public TranslateTransform HolderPosition
    {
        get { return (TranslateTransform)Holder.RenderTransform; }
    }


    public FunkyShape(Canvas playground, Shape shapeToInit)
    {
        Holder = shapeToInit;

        Holder.Width = 10;
        Holder.Height = 10;
        Holder.Fill = new SolidColorBrush(Colors.Blue);
        Holder.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
        Holder.RenderTransform = new TranslateTransform()
        {
            X = 500,
            Y = 500
        };
        Holder.RenderTransformOrigin = new Point(0.5, 0.5);

        // init done
        playground.Children.Add(Holder);

        Animate();
    }

    public void Animate()
    {
        DoubleAnimation g1 = GrowAnimation();

        Storyboard sb = new Storyboard();
        Storyboard.SetTarget(g1, this);

        // CAN'T FIND ANIMATOR PROPERTY
        Storyboard.SetTargetProperty(g1, "Animator");

        sb.Children.Add(g1);

        sb.Begin(); // THROWS EXCEPTION
    }

    private static DoubleAnimation GrowAnimation()
    {
        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(3000);
        growAnimation.From = 0;
        growAnimation.To = 100;
        growAnimation.AutoReverse = true;
        growAnimation.EnableDependentAnimation = true;
        growAnimation.RepeatBehavior = new RepeatBehavior(5);
        return growAnimation;
    }
}

但是,当我尝试创建该类的实例并将其添加到画布时,我得到了异常 - Storyboard.Being() 抛出它并告诉我它找不到 Animator 属性.

However, when I try making an instance of the class and adding it to the canvas, I get Exception - Storyboard.Being() throws it and tells me that it can't find Animator property.

那么 - 我做错了什么?

So - what am I doing wrong?

3 次代码更改后 - 它仍然无法正常工作;我收到无法解析指定对象上的 TargetProperty Animator"错误.因此,如果有人知道答案 - 请通过修改代码来提供帮助.谢谢!

After 3 code changes - it is still not working; I get "Cannot resolve TargetProperty Animator on specified object" error. So if somebody knows the answer - please help out by modifying the code. Thanks!

好的,在 24 小时的头撞墙之后,有一些进展 - 如果我通过 XAML 添加形状,它会动画,但如果我通过后面的代码添加它 (Canvas.Children.添加),它不起作用.让我看看我能不能找出原因.

OK, after 24 hours of banging head against the wall there is some progress - if I add shape through XAML it animates, but if I add it through code behind (Canvas.Children.Add), it doesn't work. Let me see if I can figure out why.

推荐答案

好的,

我已经找到了解决框架中明显存在错误的方法(尽管我确信某些 MS 员工会发布回复并说这是一项功能/它的设计).需要做几件事:

I've found the workaround for what is obviously a bug within the framework (although I'm sure some MS employee will post response and say it's a feature/it-is-by-design). Several things need to be done:

  1. 添加默认/无参数构造函数
  2. 将 FunkyShape 的基类更改为 UserControl.
  3. 打开要在其中添加形状的 Page 类的 XAML 视图
  4. 在 Canvas XAML 中添加一个 FunkyShape 实例作为子项(例如 ).没有这个就行不通.
  5. 在代码隐藏中创建一个 FunkyShape 实例,将其添加到画布中,启动动画并欣赏它的工作原理
  6. 改用错误更少的技术.

这篇关于Windows 8 - 在代码隐藏中动画自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:24