委托和事件处理的问题

时间:2011-09-12 03:51:19

标签: c# c#-4.0

在我谷歌这么多之后,我仍然无法理解代表和活动是如何运作的。

我想要达到的目标是: Class 1将播放动画,当动画END时,它会将委托/事件传递给Class 2,这样Class 2将在动画END上做一些事情。

我想知道如何编写上述行为?

4 个答案:

答案 0 :(得分:1)

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

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
    }

}

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

答案 1 :(得分:0)

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

尽管如此,在您提到的场景中,您是否可以直接从Class1中调用Class2的DoSomething?

答案 2 :(得分:0)

这是两个班级。我假设你在谈论上面的例子。

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.

答案 3 :(得分:0)

因此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();
    }