运行操作时C#WPF动画结束 [英] Run action when c# wpf animation ends

查看:137
本文介绍了运行操作时C#WPF动画结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习WPF,并在同一时间开发它的应用程序。我有一个很难搞清楚当DoubleAnimation是(或其他种类)做我如何运行的东西。例如:

I'm learning wpf and at the same time developing an app with it. I'm having a hard time figuring out how i can run something when a doubleanimation (Or other sorts) is done. For instance:

DoubleAnimation myanim = new DoubleAnimation();
myanim.From = 10;
myanim.To = 100;
myanim.Duration = new Duration(TimeSpan.FromSeconds(3));
myview.BeginAnimation(Button.OpacityPropert, myanim);

//Code to do something when animation ends

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace app
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DoubleAnimation widthbutton = new DoubleAnimation();
        widthbutton.From = 55;
        widthbutton.To = 100;
        widthbutton.Duration = new Duration(TimeSpan.FromSeconds(1.5));
        button1.BeginAnimation(Button.HeightProperty, widthbutton);

        DoubleAnimation widthbutton1 = new DoubleAnimation();
        widthbutton1.From = 155;
        widthbutton1.To = 200;
        widthbutton1.Duration = new Duration(TimeSpan.FromSeconds(1.5));
        button1.BeginAnimation(Button.WidthProperty, widthbutton1);

        widthbutton.Completed += new EventHandler(myanim_Completed);
    }
    private void myanim_Completed(object sender, EventArgs e)
    {
        //your completed action here
        MessageBox.Show("Animation done!");
    }
}
}

这是怎么回事accomplishable?我看了这个相当多的其他职位,但他们都用它XAML,但我想用C#code做解释。谢谢!

How is this accomplishable? I have read quite a few other posts about this, but they all explain it using xaml, however i would like to do it using c# code. Thanks!

推荐答案

您可以将一个事件处理程序DoubleAnimation是的<一个href=\"http://msdn.microsoft.com/en-gb/library/system.windows.media.animation.timeline.completed.aspx\">Completed事件。

You can attach an event handler to the DoubleAnimation's Completed event.

myanim.Completed += new EventHandler(myanim_Completed);

private void myanim_Completed(object sender, EventArgs e)
{
    //your completed action here
}

或者,如果你preFER它内联,你可以做

Or, if you prefer it inline, you can do

 myanim.Completed += (s,e) => 
     {
        //your completed action here
     };

记住启动动画否则它不会开火之前附加处理程序。

Remember to attach the handler before starting the animation otherwise it won't fire.

这篇关于运行操作时C#WPF动画结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆