如何申报接口的事件处理程序? [英] How to declare an event handler in an interface?
问题描述
我有几个Silverlight 4的用户界面对象(导航页更喜欢它)有实现两件事情:OnError事件处理程序,并刷新()方法。
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.
好,问题是,都应该实现这个页面被托管导航框架内,采用SL4导航框架。这是罚款和花花公子不过,我也需要能够中继子页面(如错误)内发生的事件给父页面。更过我希望能够迫使基于发生在父事件子页面UI的刷新。
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.
要规避使用反射(看什么类型在导航面板中显示的页)我想只提取IDynamic 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. 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.
}
}
对于所有你们谁是MVVM / MVC的球迷......嗯,这是不是。我知道,如果一个MVVM apprach被带到使得这款,它会一直轻松了很多,但应用程序已经写,我不打算从头开始重写。 (
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. :(
由于
马丁
推荐答案
尝试定义为事件动作< ErrorEventArgs>的OnError;
这篇关于如何申报接口的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!