委托和事件处理问题

发布于 2024-12-03 23:22:44 字数 126 浏览 2 评论 0原文

在我谷歌了这么多之后,我仍然无法理解委托和事件是如何工作的。

我想要实现的是: 类1将播放动画,当动画结束时,它将把委托/事件传递给类2,以便类2在动画结束时执行一些操作。

我想知道如何对上述行为进行编码?

After I google so much and I still cant understand how does the delegate and event works.

What I want to achieve is:
Class 1 will be playing animation, when the animation END, it will pass the delegate/event to the Class 2 so that Class 2 will do something on the animation END.

I wonder how can I code the above behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴者 2024-12-10 23:22:44

非常基本的事件实现...

public delegate void Class1AnimationCompletedHandler(object sender, EventArgs e);

public class Class1
{
    public event Class1AnimationCompletedHandler AnimationCompleted;

    private void OnAnimationCompleted(EventArgs e)
    {
        if (this.AnimationCompleted != null)
        {
            // raises the AnimationCompleted event
            this.AnimationCompleted(this, e);
        }
    }

    public void Animate()
    {
        // do your animation steps here

        this.OnAnimationCompleted(new EventArgs());
    }
}

public class Class2
{
    private Class1 c1;

    public Class2()
    {
        // initialize the class
        c1 = new Class1();

        // attach this.Class1_AnimationCompleted to the AnimationCompleted event;
        c1.AnimationCompleted += new Class1AnimationCompletedHandler(this.Class1_AnimationCompleted);
    }

    private void Class1_AnimationCompleted(object sender, EventArgs e)
    {
        // do your post-animation stuff here
    }

}

这个链接 提供了一个关于如何创建事件的快速示例。它是针对 VS 2003 的,但仍然适用。

Very basic event implementation...

public delegate void Class1AnimationCompletedHandler(object sender, EventArgs e);

public class Class1
{
    public event Class1AnimationCompletedHandler AnimationCompleted;

    private void OnAnimationCompleted(EventArgs e)
    {
        if (this.AnimationCompleted != null)
        {
            // raises the AnimationCompleted event
            this.AnimationCompleted(this, e);
        }
    }

    public void Animate()
    {
        // do your animation steps here

        this.OnAnimationCompleted(new EventArgs());
    }
}

public class Class2
{
    private Class1 c1;

    public Class2()
    {
        // initialize the class
        c1 = new Class1();

        // attach this.Class1_AnimationCompleted to the AnimationCompleted event;
        c1.AnimationCompleted += new Class1AnimationCompletedHandler(this.Class1_AnimationCompleted);
    }

    private void Class1_AnimationCompleted(object sender, EventArgs e)
    {
        // do your post-animation stuff here
    }

}

This link provides a good quick example on how to create events. It's targeted for VS 2003, but is still applicable.

寄意 2024-12-10 23:22:44

将其视为发布者(公开事件的类)和订阅者(​​向事件注册处理程序的类)。

但是,在您提到的场景中,您不能直接从 Class1 中调用 Class2 的 DoSomething 吗?

Think of it as a Publisher (the class exposing an event) and Subscriber (the class registering an handler to event).

Though, in the scenario you mentioned, can you not call the DoSomething of Class2 directly from within Class1?

嘴硬脾气大 2024-12-10 23:22:44

这里有两个类。我假设您正在谈论上面的实例。

public class ClassA
{
    public event EventHandler Finished;

    public ClassA() {}

    public void Animate()
    {
        Console.WriteLine("ClassA instance animating.");
        if (Finished != null)
            Finished(this, null);
    }
}

public class ClassB
{
    public ClassB() {}

    public void DoWork()
    {
        Console.WriteLine("ClassB instance doing work.");
    }
}

然后,如果你有一个获胜形式,你会得到:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ClassA a = new ClassA();
        a.Finished += delegate(object sender, EventArgs e)
        {
            ClassB b = new ClassB();
            b.DoWork();
        };
        a.Animate();
    }
}

输出将是:

ClassA instance animating.
ClassB instance doing work.

Here are two classes. I assume you're talking about instances above.

public class ClassA
{
    public event EventHandler Finished;

    public ClassA() {}

    public void Animate()
    {
        Console.WriteLine("ClassA instance animating.");
        if (Finished != null)
            Finished(this, null);
    }
}

public class ClassB
{
    public ClassB() {}

    public void DoWork()
    {
        Console.WriteLine("ClassB instance doing work.");
    }
}

Then, if you had a win form you'd have:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ClassA a = new ClassA();
        a.Finished += delegate(object sender, EventArgs e)
        {
            ClassB b = new ClassB();
            b.DoWork();
        };
        a.Animate();
    }
}

And the output would be:

ClassA instance animating.
ClassB instance doing work.
岁月蹉跎了容颜 2024-12-10 23:22:44

因此,Class1 将公开一个事件,Class2 将向该事件附加一个委托。当动画结束时,Class1 将引发该事件。这是你需要的吗?

    class Class1
    {
        public delegate void AnimationEndHandler();
        public event AnimationEndHandler AnimationEnded;

        public void Animation()
        {
            //do the animation
            Console.WriteLine("Animation ended. This is class 1");
            AnimationEnded();
        }
    }

    class Class2
    {
        public Class2(Class1 c1)
        {
            c1.AnimationEnded += new Class1.AnimationEndHandler(DoSomethingOnAnimationEnd);
        }

        public void DoSomethingOnAnimationEnd()
        {
            Console.WriteLine("Animation ended. This is class 2");
        }
    }

    static void Main(string[] args)
    {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2(c1);

        c1.Animation();
    }

So Class1 will expose an event, to which Class2 will attach a delegate. When the animation ends Class1 will raise the event. Is that what you need?

    class Class1
    {
        public delegate void AnimationEndHandler();
        public event AnimationEndHandler AnimationEnded;

        public void Animation()
        {
            //do the animation
            Console.WriteLine("Animation ended. This is class 1");
            AnimationEnded();
        }
    }

    class Class2
    {
        public Class2(Class1 c1)
        {
            c1.AnimationEnded += new Class1.AnimationEndHandler(DoSomethingOnAnimationEnd);
        }

        public void DoSomethingOnAnimationEnd()
        {
            Console.WriteLine("Animation ended. This is class 2");
        }
    }

    static void Main(string[] args)
    {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2(c1);

        c1.Animation();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文