8

如果有以下情况:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.Method1();
}
public class MyClass1() {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1();
    }
}
public class MyClass2() {
   public void Method1() {
       MyClass3 obj = new MyClass3();
       obj.Method1();
   }
}
public class MyClass3() {
   public void Method1() {
       // Raise event here that is handled in MyClass1?    
   }
}

可以MyClass3.Method1()引发在 中处理的事件MyClass1吗?

如果我想实现这一点,将如何编写事件处理代码?

4

5 回答 5

4

是的,它可以,但是由于每个级别都不知道您链的更深层次,因此您必须在每个类上创建事件。有些像这样:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.MyEvent += (s, e) => { Console.WriteLine("Fired!"); };
    obj.Method1();
}

public class MyClass1 {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.MyEvent += (s, e) => { OnMyEvent(); };
        obj.Method1();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}
public class MyClass2 {
    public void Method1() {
        MyClass3 obj = new MyClass3();
        obj.MyEvent += (s, e) => { OnMyEvent(); };
        obj.Method1();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}
public class MyClass3 {
    public void Method1() {
        // Raise event here that is handled in MyClass1?    
        OnMyEvent();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}
于 2012-09-03T08:07:10.467 回答
2

要链接事件处理程序,请使用 MyClass2 中的添加/删除语法。从 MyClass1 中设置 SomeEvent 并在 MyClass3 中引发它。

public class MyClass1             
{             
    MyClass2 obj = new MyClass2(); 

    public MyClass1()
    {
        obj.SomeEvent += obj_SomeEvent;
    }

    public void Method1()             
    {                      
        obj.Method1();             
    }             

    private static void obj_SomeEvent(object sender, EventArgs e)             
    {             
        Console.WriteLine("Some event fired");             
    }             
}  


public class MyClass2() 
{    
   MyClass3 cls3 = new MyClass3();

   public void Method1() 
   {     
       cls3.FireSomeEvent();    
   }   

    public event MyEventHandler SomeEvent
    { 
        add { this.cls3.SomeEvent += value; } 
        remove { this.cls3.SomeEvent -= value; } 
    }  
}

public class MyClass3() 
{
    public event EventHandler SomeEvent;

    private void OnSomeEvent() 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(this, new EventArgs()); 
        } 
    } 

    public void FireSomeEvent
    {
        OnSomeEvent();
    }
}
于 2012-09-03T08:15:32.110 回答
1

如果你想避免回调解决方案和每个类中的事件链,你基本上有2个解决方案。

第一个包括将 MyClassX 类型的局部变量转换为字段,即像 Chris Gessler 建议的那样,但完全遵循这种方法并删除局部变量。

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.c2.c3.SomeEvent += obj_SomeEvent;      
    obj.Method1();
}

private static void obj_SomeEvent(object sender, EventArgs e)             
{             
    Console.WriteLine("Some event fired");             
}

public class MyClass1() {
    public MyClass2 c2 = new MyClass2();

    public void Method1() {
        c2.Method1();
    }
}
public class MyClass2() {
   public MyClass3 c3 = new MyClass3();

   public void Method1() {
       c3.Method1();
   }
}
public class MyClass3() {
    public event EventHandler SomeEvent;

    private void OnSomeEvent() 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(this, new EventArgs()); 
        } 
    } 
   public void Method1() {
       OnSomeEvent();    
   }
}

您的另一个选择(但如果可行,这实际上取决于您要尝试做什么,而且我无论如何都不喜欢)是将 MyClass3 中的事件简单地定义为静态:

public static void main() { 
    MyClass3.SomeEvent += obj_SomeEvent;
    MyClass1 obj = new MyClass1();
    obj.Method1();
}

private static void obj_SomeEvent(object sender, EventArgs e)             
{             
     Console.WriteLine("Some event fired");             
}

public class MyClass1() {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1();
    }
}
public class MyClass2() {
   public void Method1() {
       MyClass3 obj = new MyClass3();
       obj.Method1();
   }
}
public class MyClass3() {
    public static event EventHandler SomeEvent;

    private void OnSomeEvent(MyClass3 anObj) 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(anObj, new EventArgs()); 
        } 
    }

    public void Method1() {
       OnSomeEvent(this);    
    }
}
于 2012-09-03T21:10:42.847 回答
1

事件处理 ABC 假设您有订阅者和发布者。所以你可能希望你的 MyClass3 有公共事件,而 MyClass1 订阅这个事件。

但是,在您的特定代码中,这种复杂性没有任何意义 - 使用回调函数的最简单方法:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.Method1();
}
public class MyClass1{
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1(MyEventHandler);
    }

    public void MyEventHandler() {
    //...
    }

}
public class MyClass2{
   public void Method1(Action callback) {
       MyClass3 obj = new MyClass3();
       obj.Method1(callback);
   }
}
public class MyClass3{
   public void Method1(Action callback) {
       // Raise event here that is handled in MyClass1?    
       callback();
   }
}
于 2012-09-03T08:05:31.700 回答
1

您可以将事件添加到中间类以连接事物。像这样的东西:

using System;
using System.Windows.Forms;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass1 obj = new MyClass1();
            obj.Method1();
        }
    }

    public class MyClass1
    {
        public void Method1()
        {
            MyClass2 obj = new MyClass2();
            obj.SomethingHappened += somethingHappened;
            obj.Method1();
        }

        private static void somethingHappened(object sender, EventArgs e)
        {
            Console.WriteLine("Something happened!");
        }
    }

    public class MyClass2
    {
        public void Method1()
        {
            MyClass3 obj = new MyClass3();
            obj.SomethingHappened += onSomethingHappened;
            obj.Method1();
        }

        public event EventHandler SomethingHappened;

        private void onSomethingHappened(object sender, EventArgs e)
        {
            var handler = SomethingHappened;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

    public class MyClass3
    {
        public void Method1()
        {
            onSomethingHappened();
        }

        private void onSomethingHappened()
        {
            var handler = SomethingHappened;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        public event EventHandler SomethingHappened;
    }
}

您可能要考虑的一件事是您如何处理中间类中的“发件人”参数。您可以将其设为 MyClass2(如上面的代码中所示),或者您可以像这样保留原始发件人:

private void onSomethingHappened(object sender, EventArgs e)
{
    var handler = SomethingHappened;

    if (handler != null)
    {
        handler(sender, e);
    }
}
于 2012-09-03T08:15:15.510 回答