C# 将事件重定向到自定义控件内的处理程序
我有一个自定义控件。我需要重定向事件处理程序。我已经大幅削减了代码,试图清晰地表达我要做的事情C# 将事件重定向到自定义控件内的处理程序,c#,event-handling,C#,Event Handling,我有一个自定义控件。我需要重定向事件处理程序。我已经大幅削减了代码,试图清晰地表达我要做的事情 public class RemoteDesktop : WindowsFormsHost { public event OnConnectingEventHandler OnConnecting; public delegate void OnConnectingEventHandler(object sender, EventArgs Arguments); public
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
}
}
然后,您可以在方法newRDC_OnConnecting中编写逻辑。确保OnConnectionEventHandler与newRDC_OnConnection具有相同的方法签名。您没有向我们显示引发
OnConnection事件的代码。从我们所看到的情况来看,事件从未引发,因此可以解释为什么从未调用视图模型处理程序。RDPUserControl.OnConnecting+=RemoteDesktop_OnConnecting;activeX控件在连接时引发事件。那么activeX控件连接时显示的代码在哪里?我假设它不会发生在构造函数中。
//at the constractor of the class
OnConnecting+=RDC_OnConnecting;