How to declare an event handler in an interface?

14,472

Solution 1

Create a interface IInform

public interface IInform
{
    event EventHandler Inform;
    void InformNow();
}

Create a Class ImplementIInform

public class ImplementInform : IInform
{
    public event EventHandler Inform;

    public void InformNow()
    {
        OnInformed(new EventArgs());
    }

    private void OnInformed(EventArgs eventArgs)
    {
        if (Inform != null)
        {
            Inform(this, eventArgs);
        }
    }
}

Create a Console Application like below..

   static void Main(string[] args)
    {
        IInform objInform = new ImplementInform();
        objInform.Inform +=new EventHandler(Informed);
        objInform.InformNow();
        Console.ReadLine();
    }

    private static void Informed(object sender, EventArgs e)
    {
        Console.WriteLine("Informed");
    }

Output: Informed

Solution 2

try defining it as event Action<ErrorEventArgs> OnError;

Share:
14,472
bleepzter
Author by

bleepzter

The Master of Disaster, yours truly.

Updated on June 05, 2022

Comments

  • bleepzter
    bleepzter almost 2 years

    I have a few Silverlight 4 UI objects (Navigation Pages more like it) that have to implement two things: OnError event handler, and Refresh() method.

    So I tried the following:

    public interface IDynamicUI
    {
        event EventHandler<ErrorEventArgs> OnError;
        void Refresh();
    }
    
    public class ErrorEventArgs : EventArgs
    {
        public string Message { get; set; }
        public Exception Error { get; set; }
    }
    

    but the compiler gives me errors saying that fields cannot be declared inside public interfaces.

    Well the problem is that the pages that are supposed to implement this are hosted inside a navigation frame, employing the SL4 navigation framework. That is fine and dandy however, I also need to be able to relay events that happen within the child page (like errors) to the parent page. More over I wanted to be able to force a refresh of child pages UI based on events that occur in the parent.

    To circumvent using reflection (to see what is the Type of the page displayed in the navigation panel) I wanted to just extract the IDynamic UI out of it. This would allow me to do something like this:

    public class ParentPage : Page
    {
        IDynamicUI selectedUI = null;
    
        //fires when the ChildContent frame loads a child page...
        private void ChildContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            object childPage = ChildContentFrame.Content;
            this.selectedUI = (IDynamicUI)childPage;
            this.selectedUI.OnError += new EventHandler(ChildPage_OnError);
        }
    
        private void ChildPage_OnError(object sender, ErrorEventArgs e)
        {
            //do my error handling here.
        }
    }
    

    For all of you who are fans of MVVM/MVC... well this is not it. I do know that if an MVVM apprach was taken into making this, it would've been a lot easier, however the app was already written and I am not going to rewrite it from scratch. :(

    Thanks Martin

  • AnthonyWJones
    AnthonyWJones over 12 years
    That would work fine if the consuming code only ever wanted to assign a single delegate to be invoked. As it turns out there is nothing wrong with the original code.
  • Lazy
    Lazy about 6 years
    Correct way would be to either use "Inform?.Invoke(this, eventArgs);" if you use a recent enough C# compiler or "EventHandler handler = Invoke; if (handler!=null) handler(this,eventArgs);" to make sure you won't be calling a null handler if handlers get removed by another thread between the null check and actually invoking the event handler.