如何用WPF制作左右晃动的动画效果?
- 内容介绍
- 文章标签
- 相关推荐
本文共计639个文字,预计阅读时间需要3分钟。
本文分享了WPF实现左右移动效果的代码示例。主要介绍如何使用DoubleAnimation和Storyboard实现控件或布局的左右移动(滑动)效果。以下为具体实现步骤:
1. 创建WPF窗口或用户控件: 在XAML中定义一个窗口或用户控件,例如:
xml
2. 编写C#代码: 在后台代码中,为按钮添加点击事件处理程序,使用DoubleAnimation和Storyboard实现左右移动效果。
csharp using System.Windows; using System.Windows.Media.Animation;
namespace WpfApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); moveButton.Click +=MoveButton_Click; }
private void MoveButton_Click(object sender, RoutedEventArgs e) { var animation=new DoubleAnimation { From=0, To=200, Duration=new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior=RepeatBehavior.Forever };
var storyboard=new Storyboard(); storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, moveButton); Storyboard.SetTargetProperty(animation, new PropertyPath((Canvas.Left)));
storyboard.Begin(); } } }
3. 运行程序: 运行程序后,点击按钮,即可看到按钮在左右方向上循环移动。
总结:本文通过一个简单的按钮移动示例,展示了如何使用DoubleAnimation和Storyboard在WPF中实现控件或布局的左右移动效果。这种方法适用于实现动画效果,使界面更加生动。
本文实例为大家分享了WPF实现左右移动效果展示的具体代码,供大家参考,具体内容如下
实现控件或布局的左右移动(晃动)主要用到DoubleAnimation以及Storyboard
布局代码为:
<Canvas> <Grid Width="200" Height="100" Background="MediumAquamarine" Name="GroupboxArea" Canvas.Left="100" Canvas.Top="200"/> <Button Content="Button" Height="25" Width="78" Click="Button_Click"/> </Canvas>
后台代码为:
private void Button_Click(object sender, RoutedEventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起点 DAnimation.To = 280;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(story_Completed);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void story_Completed(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 280;//起点 DAnimation.To = 100;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void storyCompleted(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起点 DAnimation.To = 200;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); //story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计639个文字,预计阅读时间需要3分钟。
本文分享了WPF实现左右移动效果的代码示例。主要介绍如何使用DoubleAnimation和Storyboard实现控件或布局的左右移动(滑动)效果。以下为具体实现步骤:
1. 创建WPF窗口或用户控件: 在XAML中定义一个窗口或用户控件,例如:
xml
2. 编写C#代码: 在后台代码中,为按钮添加点击事件处理程序,使用DoubleAnimation和Storyboard实现左右移动效果。
csharp using System.Windows; using System.Windows.Media.Animation;
namespace WpfApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); moveButton.Click +=MoveButton_Click; }
private void MoveButton_Click(object sender, RoutedEventArgs e) { var animation=new DoubleAnimation { From=0, To=200, Duration=new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior=RepeatBehavior.Forever };
var storyboard=new Storyboard(); storyboard.Children.Add(animation);
Storyboard.SetTarget(animation, moveButton); Storyboard.SetTargetProperty(animation, new PropertyPath((Canvas.Left)));
storyboard.Begin(); } } }
3. 运行程序: 运行程序后,点击按钮,即可看到按钮在左右方向上循环移动。
总结:本文通过一个简单的按钮移动示例,展示了如何使用DoubleAnimation和Storyboard在WPF中实现控件或布局的左右移动效果。这种方法适用于实现动画效果,使界面更加生动。
本文实例为大家分享了WPF实现左右移动效果展示的具体代码,供大家参考,具体内容如下
实现控件或布局的左右移动(晃动)主要用到DoubleAnimation以及Storyboard
布局代码为:
<Canvas> <Grid Width="200" Height="100" Background="MediumAquamarine" Name="GroupboxArea" Canvas.Left="100" Canvas.Top="200"/> <Button Content="Button" Height="25" Width="78" Click="Button_Click"/> </Canvas>
后台代码为:
private void Button_Click(object sender, RoutedEventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起点 DAnimation.To = 280;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(story_Completed);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void story_Completed(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 280;//起点 DAnimation.To = 100;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); } void storyCompleted(object sender, EventArgs e) { DoubleAnimation DAnimation = new DoubleAnimation(); DAnimation.From = 100;//起点 DAnimation.To = 200;//终点 DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea); Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty)); Storyboard story = new Storyboard(); //story.Completed += new EventHandler(storyCompleted);//完成后要做的事 //story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上 story.Children.Add(DAnimation); story.Begin(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

