wpf中这个简单的位置动画怎样用c#实现?

wyman25 2009-06-03 06:08:48
很简单一个动画,就是位移而已,要求用一个DoubleAnimationUsingKeyFrames,用一个TranslateTransform,并且全部用c#实现。
网上wpf相关资料都是在xaml里面实现的,但我现在只能用c#动态来实现,请各位帮帮忙。特别是Storyboard.SetTargetProperty(XOffsetAnimation, new PropertyPath());
中,new PropertyPath()怎么对应到这个TranslateTransform
...全文
257 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BossFriday 2009-06-04
  • 打赏
  • 举报
回复
去看看webcaset里的讲座吧.wpf这玩艺毕竟现在普及率还不是太高.
voodoo82 2009-06-04
  • 打赏
  • 举报
回复
给你一个Sample,把一个Rectangle水平移动200。

XAML:
---------------------------------------------------------------------------------------------------------------------
[code=XAML]
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Animation Demo" Height="270" Width="480">
<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetName="rect" TargetProperty="RenderTransform.X">
<DoubleAnimationUsingKeyFrames Duration="0:0:2" >
<LinearDoubleKeyFrame KeyTime="0:0:0" Value="0" />
<LinearDoubleKeyFrame KeyTime="0:0:2" Value="200" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Canvas.Triggers>
<Rectangle Height="20" Width="20" Name="rect" Canvas.Left="10" Canvas.Top="10">
<Rectangle.Fill>
<SolidColorBrush Color="Red" />
</Rectangle.Fill>
<Rectangle.RenderTransform>
<TranslateTransform X="0" Y="0" />
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Window>
[/code]
---------------------------------------------------------------------------------------------------------------------------

C#实现同样的效果
---------------------------------------------------------------------------------------------------------------------------

public Window1()
{
InitializeComponent();

Rectangle rect = new Rectangle();
rect.Height = 20;
rect.Width = 20;
rect.Fill = new SolidColorBrush(Colors.Red);
rect.RenderTransform = new TranslateTransform(0, 0);

canvas.Children.Add(rect);

DoubleAnimationUsingKeyFrames animation =
new DoubleAnimationUsingKeyFrames();
LinearDoubleKeyFrame firstFrame = new LinearDoubleKeyFrame(0,
KeyTime.FromPercent(0));
LinearDoubleKeyFrame secondFrame = new LinearDoubleKeyFrame(200,
KeyTime.FromTimeSpan(new TimeSpan(0, 0, 2)));

animation.KeyFrames.Add(firstFrame);
animation.KeyFrames.Add(secondFrame);

TranslateTransform tt = rect.RenderTransform as TranslateTransform;
tt.BeginAnimation(TranslateTransform.XProperty, animation);
}

--------------------------------------------------------------------------------------------------------------------
wyman25 2009-06-03
  • 打赏
  • 举报
回复
up下

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧