gpt4 book ai didi

c# - c# wpf 动画结束时运行 Action

转载 作者:可可西里 更新时间:2023-11-01 08:53:21 24 4
gpt4 key购买 nike

我正在学习 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!");
}
}
}

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

最佳答案

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

myanim.Completed += new EventHandler(myanim_Completed);

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

或者,如果你更喜欢内联,你可以这样做

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

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

关于c# - c# wpf 动画结束时运行 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com