【发布时间】:2010-05-28 19:05:17
【问题描述】:
作为一个展览,我正在尝试在 ScaleTransform 的 ScaleX 和 ScaleY 属性上使用 DoubleAnimation。我有一个矩形 (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)"; 到底是什么?这些数字代表什么?
【问题讨论】:
标签: c# .net wpf wpf-controls