具有参数Catch和句柄的C#自定义EventHandler-> RoutedEvent null

时间:2018-07-05 11:23:58

标签: c# event-handling parameter-passing handle eventhandler

我有以下处理程序:

public class RoutedEventArgsParam : RoutedEventArgs
{
    private readonly object _param1;

    public object param1
    {
        get { return _param1; }
    }

    public RoutedEventArgsParam(object param1)
    {
        this._param1 = param1;
    }
}


public delegate void RoutedEventHandlerParam(object sender, RoutedEventArgsParam e);

我想这样引发并抓住这一事件:

注册和事件抛出:

public static event RoutedEventHandlerParam popUpClosedEvent;

在我知道弹出窗口即将关闭的地方:

popUpClosedEvent?.Invoke(this, new RoutedEventArgsParam("ExampleString_Closed"));

在捕获类的构造函数中:

MyPopupPage.popUpClosedEvent += new RoutedEventHandlerParam(popUpClosed);

和方法:

    private void popUpClosed(object sender, RoutedEventArgsParam e)
    {
        WPFLibrary.UserControls.MyPopupPage ctrl = sender as WPFLibrary.UserControls.MyPopupPage;
        if (ctrl != null && ctrl.DataContext != null && ctrl.DataContext.GetType() == GetType() && (ctrl.DataContext as MyVM).vmGuid == vmGuid)
        {
            loadIconEnd();
            if (e != null)
                e.Handled = true;
        }
    }

一切正常,我得到了所有参数,但是..我不能使用e.handled = true;

比我得到例外:

Every RoutedEventArgs must have a non-null RoutedEvent associated with it.

我很好,但是我不知道如何在我的场景中实现RoutedEvent ...

0 个答案:

没有答案