c# wpf 动画结束时运行动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15795850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:27:44  来源:igfitidea点击:

Run action when c# wpf animation ends

c#wpfanimationdoubleanimation

提问by user1446632

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:

我正在学习 wpf,同时用它开发一个应用程序。当双重动画(或其他类型)完成时,我很难弄清楚如何运行某些东西。例如:

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!");
    }
}
}

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!

这是如何实现的?我已经阅读了很多关于此的其他帖子,但它们都使用 xaml 进行了解释,但是我想使用 c# 代码来进行。谢谢!

回答by keyboardP

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

您可以将事件处理程序附加到 DoubleAnimation 的Completed事件。

myanim.Completed += new EventHandler(myanim_Completed);

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

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.

请记住在开始动画之前附加处理程序,否则它不会触发。