StoryBoard使用,Xaml转cs代码
<EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="image1"
Storyboard.TargetProperty="(Canvas.Left)" >
<DoubleAnimation AutoReverse="True"
RepeatBehavior="Forever"
BeginTime="0"
Duration="0:0:20" From="0" To="-1100">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Canvas.Triggers>
转换为cs代码:
this.image1.Name = "image1";
NameScope.SetNameScope(this,new NameScope());
this.RegisterName(this.image1.Name,this.image1);
DoubleAnimation xAnimation = new DoubleAnimation();
xAnimation.From = 0;
xAnimation.To = -1100;
xAnimation.Duration = new Duration(TimeSpan.FromSeconds(30));
DependencyProperty[] propertyChain = new DependencyProperty[]
{
Image.RenderTransformProperty,
TranslateTransform.XProperty
};
Storyboard story = new Storyboard();
story.AutoReverse = true;
story.RepeatBehavior = RepeatBehavior.Forever;
story.Children.Add(xAnimation);
Storyboard.SetTargetName(xAnimation,this.image1.Name);
Storyboard.SetTargetProperty(xAnimation,new PropertyPath("(0).(1)",propertyChain));
story.Begin(this);