简体   繁体   English

如何在接口中声明事件处理程序?

[英]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. 我有一些Silverlight 4 UI对象(更像它的“导航页面”)必须实现两件事:OnError事件处理程序和Refresh()方法。

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. 问题是,应该实现此目的的页面使用SL4导航框架托管在导航框架内。 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. 此外,我希望能够基于父级中发生的事件来强制刷新子页面UI。

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. 为了避免使用反射(以查看导航面板中显示的页面的类型是什么),我想从中提取IDynamic UI。 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. 对于所有喜欢MVVM / MVC的人...事实并非如此。 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. 我确实知道,如果要使用MVVM方法进行操作,这会容易得多,但是该应用程序已经编写,因此我不会从头开始重写它。 :( :(

Thanks Martin 谢谢马丁

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

Create a interface IInform 创建一个接口IInform

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

Create a Class ImplementIInform 创建一个类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 输出:通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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