简体   繁体   English

如何在引发其他事件时举起活动?

[英]How to raise an event when another event is raised?

I have an application that handles the OnQuit event of another running application. 我有一个应用程序来处理另一个正在运行的应用程序的OnQuit事件。 I would like to raise an additional (custom) event when the OnQuit event is handled. 我想在处理OnQuit事件时引发一个额外的(自定义)事件。 How could I implement such an event? 我怎么能实现这样的活动?

My OnQuit handler is like so: 我的OnQuit处理程序是这样的:

    private void StkQuit()
    {
        _stkApplicationUi.OnQuit -= StkQuit;
        Marshal.FinalReleaseComObject(_stkApplicationUi);
        Application.Exit();
    }

The reason I require the additional event is so that I can tell my View layer that the application has exited. 我需要附加事件的原因是我可以告诉我的View层应用程序已退出。 If this is not the correct way, what would be better? 如果这不是正确的方法,那会更好吗?

WulfgarPro WulfgarPro

I will usually have an event in my view interface like so: 我通常会在我的视图界面中有一个事件,如下所示:

public interface ITestView
    {
        event EventHandler OnSomeEvent;
    }

Then from a presenter constructor I'll wire up those events: 然后从演示者构造函数中我将连接这些事件:

public class TestPresenter : Presenter
{
    ITestView _view;

    public TestPresenter(ITestView view)
    {
        _view.OnSomeEvent += new EventHandler(_view_OnSomeEvent);
    }

    void _view_OnSomeEvent(object sender, EventArgs e)
    {
        //code that will run when your StkQuit method is executed
    }
}

And from your aspx codebehind: 从你的aspx代码隐藏:

public partial class Test: ITestView
{
     public event EventHandler OnSomeEvent;
     public event EventHandler OnAnotherEvent;

    private void StkQuit()
    {
        _stkApplicationUi.OnQuit -= StkQuit;
        Marshal.FinalReleaseComObject(_stkApplicationUi);
        if (this.OnSomeEvent != null)
        {
            this.OnSomeEvent(this, EventArgs.Empty);
        }
        Application.Exit();
    }
}

Hope that helps!! 希望有所帮助!!

Just register this additional event with the _stkApplication after OnQuit has been registered. 在OnQuit注册后,只需使用_stkApplication注册此附加事件。

_stkApplicationUi.OnQuit += StkQuit;
_stkApplicationUi.OnQuitAdditional += AddlQuitHandler;

where AddlQuitHandler is the handler for the custom event 其中AddlQuitHandler是自定义事件的处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM