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

Indudhar Gowda 426 Reputation points
2020-04-03T18:41:01.687+00:00

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.

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-06T02:29:06.633+00:00

    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.

    0 comments No comments