• <legend id='mTOo0'><style id='mTOo0'><dir id='mTOo0'><q id='mTOo0'></q></dir></style></legend>
    <i id='mTOo0'><tr id='mTOo0'><dt id='mTOo0'><q id='mTOo0'><span id='mTOo0'><b id='mTOo0'><form id='mTOo0'><ins id='mTOo0'></ins><ul id='mTOo0'></ul><sub id='mTOo0'></sub></form><legend id='mTOo0'></legend><bdo id='mTOo0'><pre id='mTOo0'><center id='mTOo0'></center></pre></bdo></b><th id='mTOo0'></th></span></q></dt></tr></i><div id='mTOo0'><tfoot id='mTOo0'></tfoot><dl id='mTOo0'><fieldset id='mTOo0'></fieldset></dl></div>
        <bdo id='mTOo0'></bdo><ul id='mTOo0'></ul>
    1. <tfoot id='mTOo0'></tfoot>

        <small id='mTOo0'></small><noframes id='mTOo0'>

        当另一个类的事件被触发时通知

        时间:2023-09-15

        <legend id='fnLgY'><style id='fnLgY'><dir id='fnLgY'><q id='fnLgY'></q></dir></style></legend>
          <tbody id='fnLgY'></tbody>

            <small id='fnLgY'></small><noframes id='fnLgY'>

            <tfoot id='fnLgY'></tfoot>
              <bdo id='fnLgY'></bdo><ul id='fnLgY'></ul>
              • <i id='fnLgY'><tr id='fnLgY'><dt id='fnLgY'><q id='fnLgY'><span id='fnLgY'><b id='fnLgY'><form id='fnLgY'><ins id='fnLgY'></ins><ul id='fnLgY'></ul><sub id='fnLgY'></sub></form><legend id='fnLgY'></legend><bdo id='fnLgY'><pre id='fnLgY'><center id='fnLgY'></center></pre></bdo></b><th id='fnLgY'></th></span></q></dt></tr></i><div id='fnLgY'><tfoot id='fnLgY'></tfoot><dl id='fnLgY'><fieldset id='fnLgY'></fieldset></dl></div>
                  本文介绍了当另一个类的事件被触发时通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have

                  class A
                  {
                      B b;
                  
                      //call this Method when b.Button_click or b.someMethod is launched
                      private void MyMethod()
                      {            
                      }
                  
                      ??
                  }
                  
                  Class B
                  {
                      //here i.e. a button is pressed and in Class A 
                      //i want to call also MyMethod() in Class A after the button is pressed 
                      private void Button_Click(object o, EventArgs s)
                      {
                           SomeMethod();
                      }
                  
                      public void SomeMethod()
                      {           
                      }
                  
                      ??
                  }
                  

                  Class A has a instance of Class B.

                  How can this be done?

                  解决方案

                  You'll need to declare a public event on class 'B' - and have class 'A' subscribe to it:

                  Something like this:

                  class B
                  {
                      //A public event for listeners to subscribe to
                      public event EventHandler SomethingHappened;
                  
                      private void Button_Click(object o, EventArgs s)
                      {
                          //Fire the event - notifying all subscribers
                          if(SomethingHappened != null)
                              SomethingHappened(this, null);
                      }
                  ....
                  
                  class A
                  {
                      //Where B is used - subscribe to it's public event
                      public A()
                      {
                          B objectToSubscribeTo = new B();
                          objectToSubscribeTo.SomethingHappened += HandleSomethingHappening;
                      }
                  
                      public void HandleSomethingHappening(object sender, EventArgs e)
                      {
                          //Do something here
                      }
                  
                  ....
                  

                  这篇关于当另一个类的事件被触发时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何修复事件延迟 下一篇:在 C# 中,为什么我不能测试事件处理程序在它定义的类之外的任何地方是否为空?

                  相关文章

                  <i id='y5cKU'><tr id='y5cKU'><dt id='y5cKU'><q id='y5cKU'><span id='y5cKU'><b id='y5cKU'><form id='y5cKU'><ins id='y5cKU'></ins><ul id='y5cKU'></ul><sub id='y5cKU'></sub></form><legend id='y5cKU'></legend><bdo id='y5cKU'><pre id='y5cKU'><center id='y5cKU'></center></pre></bdo></b><th id='y5cKU'></th></span></q></dt></tr></i><div id='y5cKU'><tfoot id='y5cKU'></tfoot><dl id='y5cKU'><fieldset id='y5cKU'></fieldset></dl></div>
                  <legend id='y5cKU'><style id='y5cKU'><dir id='y5cKU'><q id='y5cKU'></q></dir></style></legend>

                  • <bdo id='y5cKU'></bdo><ul id='y5cKU'></ul>

                  <small id='y5cKU'></small><noframes id='y5cKU'>

                    <tfoot id='y5cKU'></tfoot>