[英]DoubleAnimation in ScaleTransform
I'm trying, as an exhibition, to use a DoubleAnimation on the ScaleX and ScaleY properties of a ScaleTransform. 作为展览,我正在尝试在ScaleTransform的ScaleX和ScaleY属性上使用DoubleAnimation。 I have a rectangle (144x144) which I want to make rectangular over five seconds.
我有一个矩形(144x144),我想在五秒钟内制作矩形。
My XAML: 我的XAML:
<Window x:Class="ScaleTransformTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<Rectangle Name="rect1" Width="144" Height="144" Fill="Aqua">
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</Window>
My C#: 我的C#:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ScaleTransform scaly = new ScaleTransform(1, 1);
rect1.RenderTransform = scaly;
Duration mytime = new Duration(TimeSpan.FromSeconds(5));
Storyboard sb = new Storyboard();
DoubleAnimation danim1 = new DoubleAnimation(1, 1.5, mytime);
DoubleAnimation danim2 = new DoubleAnimation(1, 0.5, mytime);
sb.Children.Add(danim1);
sb.Children.Add(danim2);
Storyboard.SetTarget(danim1, scaly);
Storyboard.SetTargetProperty(danim1, new PropertyPath(ScaleTransform.ScaleXProperty));
Storyboard.SetTarget(danim2, scaly);
Storyboard.SetTargetProperty(danim2, new PropertyPath(ScaleTransform.ScaleYProperty));
sb.Begin();
}
Unfortunately, when I run this program, it does nothing. 不幸的是,当我运行这个程序时,它什么也没做。 The rectangle stays at 144x144.
矩形保持在144x144。 If I do away with the animation, and just
如果我取消动画,只是
ScaleTransform scaly = new ScaleTransform(1.5, 0.5);
rect1.RenderTransform = scaly;
it will elongate it instantly, no problem. 它会立即拉长它,没问题。 There is a problem elsewhere.
其他地方有问题。 Any suggestions?
有什么建议? I have read the discussion at http://www.eggheadcafe.com/software/aspnet/29220878/how-to-animate-tofrom-an.aspx in which someone seems to have gotten a pure-XAML version working, but the code is not shown there.
我已经阅读了http://www.eggheadcafe.com/software/aspnet/29220878/how-to-animate-tofrom-an.aspx上的讨论,其中有人似乎已经获得了纯XAML版本,但代码在那里没有显示。
EDIT: At Applying animated ScaleTransform in code problem it seems someone had a very similar problem, I am fine with using his method that worked, but what the heck is that string thePath = "(0).(1)[0].(2)";
编辑:在代码问题中应用动画ScaleTransform似乎有人有一个非常类似的问题,我很好用他的方法工作,但是什么是
string thePath = "(0).(1)[0].(2)";
all about? 所有关于? What are those numbers representing?
这些数字代表什么?
Here's the deal, this is a quote from MSDN's Storyboards Overview entry, in the section titled 'Where Can You Use a Storyboard?': 这是交易,这是MSDN的故事板概述条目引用的标题为“你在哪里可以使用故事板?”的部分:
A Storyboard can be used to animate dependency properties of animatable classes (for more information about what makes a class animatable, see the Animation Overview).
故事板可用于为可动画类的依赖属性设置动画(有关使类可动画的内容的更多信息,请参阅动画概述)。 However, because storyboarding is a framework-level feature, the object must belong to the NameScope of a FrameworkElement or a FrameworkContentElement.
但是,由于故事板是一种框架级功能,因此该对象必须属于FrameworkElement的NameScope或FrameworkContentElement。
This got me thinking that the ScaleTransform
object does not belong to the NameScope
of any FrameworkElement
. 这让我觉得
ScaleTransform
对象不属于任何FrameworkElement
的NameScope
。 Even though the Rectangle
is a FrameworkElement
, since the ScaleTransform
is not part of its logical children, but rather a value assigned to some other property (in this case the RenderTransform
property). 即使
Rectangle
是FrameworkElement
,因为ScaleTransform
不是其逻辑子ScaleTransform
的一部分,而是分配给其他属性的值(在本例中为RenderTransform
属性)。
To get around that you need to specify your target object and PropertyPath
differently, thus: 要解决这个问题,您需要以不同方式指定目标对象和
PropertyPath
,因此:
Storyboard.SetTarget(danim1, rect1);
Storyboard.SetTargetProperty(danim1,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
Tried it and it works, even though I don't fully understand the quote from MSDN myself :-) 虽然我不完全理解MSDN自己的引用,但尝试了它并且它有效:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.