事件处理程序无效

时间:2013-08-26 22:32:59

标签: c# .net wpf event-handling

MainWindow构造函数中,我正在注册一个事件处理程序:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        pageViewDocText = new PageViewDocText();
        framePageDocFieldDetail.Content = pageViewDocText;
        pageViewDocText.NewPageIRPRO += new GabeLib.SearchCls.DocEventHandler(ViewIPRO);
    }

    protected void ViewIPRO(string IRPOlink) // ...
}

public partial class PageViewDocText : Page, INotifyPropertyChanged
{
    public event GabeLib.SearchCls.DocEventHandler NewPageIRPRO;

    private void btn_PageBreakNext(object sender, RoutedEventArgs e)
    {
        // this fires but NewPageIRPRO is null
        if (NewPageIRPRO != null)
        {
            NewPageIRPRO("dummylink");
        }
    }
}

但在PageViewDocText中,NewPageIRPRO为空

我做错了什么? 从PageViewDocText开始,我想致电MainWindow.ViewIPRO

此事件处理程序触发并注册在pageViewDocText.NewPageIRPRO + =

下面的行
App.StaticGabeLib.Search.NewDocIRPRO += new GabeLib.SearchCls.DocEventHandler(ViewIPRO);

1 个答案:

答案 0 :(得分:1)

看起来你正在调用来自另一个类(未列出)的事件,并且缺少一个委托。

试试这段代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        pageViewDocText = new PageViewDocText();
        framePageDocFieldDetail.Content = pageViewDocText;
        pageViewDocText.PageBreakNext += new PageBreakNext(ViewIPRO);
    }

    protected void ViewIPRO(string IRPOlink) // ...
}

public partial class PageViewDocText : Page, INotifyPropertyChanged
{
    public delegate void PageBreakNext(string IRPOlink);
    public event PageBreakNext PageBreak;

    private void btn_PageBreakNext(object sender, RoutedEventArgs e)
    {
        // this fires but NewPageIRPRO is null
        if (PageBreak != null)
        {
            PageBreak("dummylink");
        }
    }
}