Windows 8的 - 在代码隐藏动画自定义属性 [英] Windows 8 - Animating custom property in code-behind
问题描述
基本上,我要让一堆形状,并使其动画。所以,我想出了以下自定义类:
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()抛出它,并告诉我,它无法找到动画属性。
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动画。所以,如果有人知道了答案 - 请修改代码助阵。 !谢谢
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.Add)代码添加它,这是行不通的。让我看看,如果我能找出原因。
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.
推荐答案
OK,
我发现什么明显的是框架内的一个错误(虽然我敢肯定有些员工MS将发布响应,并说这是一个功能/ IT-是按设计)的解决方法。有几件事情需要做:
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:
- 添加默认/无参数的构造函数
- 更改基类FunkyShape到用户控件。
- 打开了Page类的XAML视图,您要添加的形状
- 添加FunkyShape的一个实例为内子画布XAML(小于TM:FunkyShape />作为例子)。 也不会工作,没有这一点。
- 在制作FunkyShape的实例代码隐藏,将其添加到画布上,启动动画和喜欢看到它的工作原理
- 切换到bug更少的技术。
- Add default/parameter-less constructor
- Change base class of FunkyShape to UserControl.
- Open up XAML view of the Page class where you want to add shapes
- Add one instance of FunkyShape as a child within the Canvas XAML (<tm:FunkyShape /> for example). IT WON'T WORK WITHOUT THIS.
- Make an instance of FunkyShape in code-behind, add it to canvas, start animation and enjoy seeing it works
- Switch to less buggy technology.
这篇关于Windows 8的 - 在代码隐藏动画自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!