System.Drawing.ImageAnimator.Animate和System.Drawing.ImageAnimator.StopAnimate解释 [英] System.Drawing.ImageAnimator.Animate and System.Drawing.ImageAnimator.StopAnimate explanation

查看:269
本文介绍了System.Drawing.ImageAnimator.Animate和System.Drawing.ImageAnimator.StopAnimate解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个语法从另一篇文章( C#如何阻止不断循环 gif动画),但似乎我无法理解这一点。什么是

i found this syntax from another article(C# How to stop animated gif from continually looping) but it seems i cant understand it. what is the meaning or purpose of s and e from

System.Drawing.ImageAnimator.Animate(txImage.Image, (s,e) => OnFrameChanged(s,e));


// start
System.Drawing.ImageAnimator.Animate(txImage.Image, (s,e) => OnFrameChanged(s,e));

// stop
System.Drawing.ImageAnimator.StopAnimate(txImage.Image, (s, e) => OnFrameChanged(s, e));

private void OnFrameChanged(object sender, EventArgs e)
{
// frame change
}

或只是任何人都可以解释这短暂。遗憾的是愚蠢的,但即时通讯真正的新节目,但我真的想学习谢谢

or simply can anyone explain this briefly. sorry for being stupid but im really new to programming but i really want to learn thank you

推荐答案

有其中三种基本方式你可以写一个事件处理程序。不幸的是代码的作者得到它非常不适当地把它们组合起来是错误的。他应该用的是原来的C#版本1的方式:

There are three basic ways in which you can write an event handler. Unfortunately the author of that code got it pretty wrong by inappropriately mixing them up. What he should have used is the original C# version 1 way:

ImageAnimator.Animate(txImage.Image, OnFrameChanged);



这是相当直接的,容易理解。当然,你应该强烈希望在这种情况下,语法,这使得它很容易调用StopAnimate()方法。要回答你的问题,我需要显示其他两种方式,你不应该使用的。在C#版本2,匿名委托可用于编写事件处理程序就地代码:

Which is quite straight-forward and easy to understand. Certainly the syntax you should strongly prefer in this case, it makes it very easy to call the StopAnimate() method. To answer your question, I need to show the other two ways, the ones you should not use. In C# version 2, an anonymous delegate can be used to write the code for the event handler in-place:

ImageAnimator.Animate(txImage.Image, delegate {
    // Put the OnFrameChanged code here...
});

在C#版本3,lambda表达式成为可写的事件处理程序就地:

In C# version 3, lambda expressions became available to write an event handler in-place:

ImageAnimator.Animate(txImage.Image, (s, e) => {
    // Put the OnFrameChanged code here...
});



这就是你问什么。 lambda表达式的(S,E)的部分代表传递给事件处理程序的两个参数,s是的发送的,e是EventArgs对象。做笔记所以lambda语法是多余的,你不实际的使用的这两个参数在OnFrameChange代码,匿名委托作品一样好。尽管许多C#程序员使用它们停下来,更喜欢到处使用lambda表达式语法。这是公平的。即使你不使用参数,您仍然必须写他们说服编译器,你的拉姆达是委托一个合适的替代品。就像你仍然不得不写有两个参数OnFrameChanged使编译器高兴。

Which is what you asked about. The (s, e) part of the lambda expression represent the two arguments that are passed to the event handler, s is the sender, e is the EventArgs object. Do note that you don't actually use those two arguments in your OnFrameChange code so the lambda syntax is superfluous, the anonymous delegate works just as well. Albeit that many C# programmers have stopped using them and prefer to use the lambda expression syntax everywhere. Which is fair. Even though you don't use the arguments, you must still write them to convince the compiler that your lambda is a proper substitute for the delegate. Much like you still had to write OnFrameChanged with two arguments to keep the compiler happy.

了解lambda表达式语法可能有点速度凹凸,任何像样的入门书关于C#语言会做的更好的工作比我能做些什么来解释它。

Understanding lambda expression syntax can be a bit of a speed-bump, any decent introductory book about the C# language will do a better job than I can do to explain it.

最后但并非最不重要的,你会发现在的this回答向您展示如何暂停在PictureBox动画,而无需在所有使用ImageAnimator类。尽管有一些机会,这只是增加了更多的问题:)

Last but not least, you'll find some hackorama code in this answer to show you how to pause an animation in a PictureBox without having to use the ImageAnimator class at all. Albeit with some odds that this just adds more questions :)

这篇关于System.Drawing.ImageAnimator.Animate和System.Drawing.ImageAnimator.StopAnimate解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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