作为展示,我正在尝试在DoubleAnimation的ScaleX和ScaleY属性上使用ScaleTransform。我有一个矩形(144x144),我想让它在5秒内变成矩形。
我的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>我的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();
}不幸的是,当我运行这个程序时,它什么也不做。矩形保持为144x144。如果我去掉动画,只是
ScaleTransform scaly = new ScaleTransform(1.5, 0.5);
rect1.RenderTransform = scaly;它会立即拉长它,没问题。其他地方也有问题。有什么建议吗?我在http://www.eggheadcafe.com/software/aspnet/29220878/how-to-animate-tofrom-an.aspx上读过一个讨论,其中有人似乎已经得到了一个纯XAML版本,但代码没有在那里显示。
编辑:在Applying animated ScaleTransform in code problem,似乎有人遇到了非常类似的问题,我对使用他的方法很好,但是string thePath = "(0).(1)[0].(2)";到底是怎么回事?这些数字代表什么?
发布于 2010-05-29 03:48:37
这是一个交易,这是从微软的Storyboards Overview条目中引用的,在标题为“在哪里可以使用故事板?”的部分:
序列图像板可用于为可设置动画的类的依存关系属性设置动画(有关使类可设置动画的详细信息,请参见动画概述)。但是,由于情节提要是框架级别的功能,因此对象必须属于FrameworkElement或FrameworkContentElement的NameScope。
这让我想到ScaleTransform对象不属于任何FrameworkElement的NameScope。即使Rectangle是FrameworkElement,因为ScaleTransform不是其逻辑子对象的一部分,而是分配给其他某个属性(在本例中为RenderTransform属性)的值。
为了解决这个问题,您需要以不同的方式指定目标对象和PropertyPath,如下所示:
Storyboard.SetTarget(danim1, rect1);
Storyboard.SetTargetProperty(danim1,
new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));尽管我自己并不完全理解MSDN中的话,但我试过了,它起作用了:-)
https://stackoverflow.com/questions/2931792
复制相似问题