简体   繁体   中英

How to declare an event handler in an interface?

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

尝试将其定义为event Action<ErrorEventArgs> OnError;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM