将事件重定向到自定义控件内的处理程序

时间:2014-08-27 01:14:33

标签: c# event-handling

我疯了一个自定义控件。我需要重定向事件处理程序。我已经大大减少了代码,试图表达我想要做的事情。

public class RemoteDesktop : WindowsFormsHost
{
    public event OnConnectingEventHandler OnConnecting;
    public delegate void OnConnectingEventHandler(object sender, EventArgs Arguments);

    public event OnDisconnectingEventHandler OnDisconnecting;
    public delegate void OnDisconnectingEventHandler(Object sender, IMsTscAxEvents_OnDisconnectedEvent Arguments);

    private AxMsRdpClient7NotSafeForScripting RDPUserControl = new AxMsRdpClient7NotSafeForScripting();
    public RemoteDesktop()
    {
        this.RDPUserControl.BeginInit();
        this.RDPUserControl.SuspendLayout();
        base.Child = RDPUserControl;
        this.RDPUserControl.ResumeLayout();
        this.RDPUserControl.EndInit();
    }
}
public class RemoteDesktopViewModel
{
    public RemoteDesktopViewModel()
    {
        RemoteDesktop newRDC = new RemoteDesktop();
        newRDC.OnConnecting += new RemoteDesktop.OnConnectingEventHandler(newRDC_OnConnecting);
    }

    void newRDC_OnConnecting(object sender, EventArgs Arguments)
    {
        //DoStuff
    }
}

基本上它一切正常,我可以连接和断开连接到远程计算机但是我无法在我的视图模型中发生被触发的事件。

任何人都可以帮我弄明白我如何正确地指出我的事件。 谢谢。

感谢一些帮助,我得到了解决方案 步骤1: 在类外声明委托(在命名空间内) 第2步: 声明要为控件调用的事件。 第3步:使用控件的事件处理程序来重新设置您创建的代理

已完成的代码

 public delegate void OnConnectingEventHandler(object sender, EventArgs Arguments);
 public delegate void OnDisconnectingEventHandler(Object sender,IMsTscAxEvents_OnDisconnectedEvent Arguments);

public class RemoteDesktop : WindowsFormsHost
{
    public event OnConnectingEventHandler IsConnecting;
    public event OnDisconnectingEventHandler IsDisconnecting;

    private AxMsRdpClient7NotSafeForScripting RDPUserControl = new AxMsRdpClient7NotSafeForScripting();
    public RemoteDesktop()
    {
        this.RDPUserControl.BeginInit();
        this.RDPUserControl.SuspendLayout();
        base.Child = RDPUserControl;
        this.RDPUserControl.ResumeLayout();
        this.RDPUserControl.EndInit();

        RDPUserControl.OnConnecting += RemoteDesktop_OnConnecting;
        RDPUserControl.OnDisconnected += RDPUserControl_OnDisconnected;
    }
    void RDPUserControl_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
    {
        IsDisconnecting(sender, e);
    }

    void RemoteDesktop_OnConnecting(object sender, EventArgs Arguments)
    {
        IsConnecting(sender, Arguments);
    }
}
public class RemoteDesktopViewModel
{
    public RemoteDesktopViewModel()
    {
        RemoteDesktop newRDC = new RemoteDesktop();
        newRDC.IsConnecting += new RemoteDesktop.OnConnectingEventHandler(newRDC_OnConnecting);
    }

    void newRDC_OnConnecting(object sender, EventArgs Arguments)
    {
        //DoStuff
    }
}

1 个答案:

答案 0 :(得分:0)

//at the constractor of the class
OnConnecting+=RDC_OnConnecting;

然后您可以在方法中编写逻辑:newRDC_OnConnecting。确保OnConnectingEventHandler与newRDC_OnConnecting具有相同的方法签名。