question

IDGO-6443 avatar image
IDGO-6443 asked ·

c# : Get EventHandler Change Notification without Creating Instance or Making it static

c# : Get EventHandler Change Notification without Creating Instance or Making it static

 Public class A
 {
   public  EventHandler KeyboardCloseEventHandlerTest;
      
      
    private void someMethod(object sender, EventArgs e)
         {             
             KeyboardCloseEventHandlerTest?.Invoke(sender, e);           
         }
 }
    
 Public class B
 {
 public  EventHandler KeyboardCloseEventHandlerTest1;
    
     private void Tesssss(object sender, EventArgs e)
     {             
     }
    
 }

I need class A EventHandler to registered in Class B and Fire Class Method.

I can't make anything static not create a instance. More Importantly it should not cause performance Issue.

windows-uwpwindows-wpf
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is there any other way... Other there is no way to solve this problem

0 Votes 0 · ·

1 Answer

AlexLi-MSFT avatar image
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft platform!

You can use unity container ,example

 using Unity;

  public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
         }
     }
     public interface IClass
     {
         void method();
     }
    
     public class A : IClass
     {
         public void method()
         {
             MessageBox.Show("A");
         }
     }
    
     public class B : IClass
     {
         public IClass IClass { get; set; }
         UnityContainer container = new UnityContainer();
         //create Class A
         IClass classA;
         public B()
         {
             container.RegisterType<IClass, A>("NameA");
             classA = container.Resolve<IClass>("NameA");
         }
    
            
          
         public void method()
         {
             MessageBox.Show("B");
         }
     }

Thanks.

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.