1

我有一个使用报告事件的组件的存储库。

我想在前端显示报告的事件。

这是存储库:

public interface IXmlRepository
{
    //irrelevant stuff removed
    event EventHandler TraceEventHandler;
}

public class XmlPanelRepository : IXmlRepository
{
    public XmlPanelRepository()
    {
        public event EventHandler TraceEventHandler;

        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    // I want to bubble the trace events up to my UI.
        TraceEventHandler += TraceEventHandler_Tracing;
    }

    private void TraceEventHandler_Tracing(object sender, EventArgs e)
    {
        // what do I do here?
    }

    void panelCom_Trace(string message)
    {
        if (TraceEventHandler!= null) TraceEventHandler.Invoke(this, new EventArgs());
    }
}

我的 UI 使用 Service 与存储库交互。服务定义为:

public interface IXmlConfigurationService
{
    //irrelevant stuff removed

    event EventHandler TraceEventHandler;
}

public class XmlConfigurationService : IXmlConfigurationService
{
    public event EventHandler TraceEventHandler;

    public XmlConfigurationService(IXmlRepository configurationRepository)
    {
        _configurationRepository.TraceEventHandler += ConfigurationRepository_TraceEventHandler;
    }

    void ConfigurationRepository_TraceEventHandler(object sender, EventArgs e)
    {
        // this never gets hit.
        if (TraceEventHandler != null) TraceEventHandler.Invoke(sender, e);
    }
}

如果我能做到这一点,我想我可以按照相同的步骤来让 UI 显示事件报告。

如何让服务报告存储库中发生的事件?

4

2 回答 2

0

如果我理解正确,您将需要完成您的活动。这类似于 Observable 实现了观察者模式中的所有监听观察者。但是对于这样写的事件:

public XmlPanelRepository()
    {
        public event EventHandler TraceEventHandler;

        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    // I want to bubble the trace events up to my UI.
        TraceEventHandler += TraceEventHandler_Tracing;
    }

    private void TraceEventHandler_Tracing(object sender, EventArgs e)
    {
        if (TraceEventHandler != null)
        {
              TraceEventHandler(this, e);
        }

    }

但是,也许您应该为该事件命名,而不是TraceEventHandler因为您现在正在公开一个事件(通常以过去时表示)。

还值得注意的是,您可以在冒泡时更改事件参数(如果您愿意)。 EventHandler(TEventArgs)可以帮助解决这个问题。

于 2012-04-26T11:56:14.463 回答
-1

您忘记使用代表...尝试这样的事情:

public interface IXmlRepository
{
   //irrelevant stuff removed
   event EventHandler TraceEventHandler;
}

public class XmlPanelRepository : IXmlRepository
{
    public delegate void EventHandler(string parameter1, string parameter2);

    public event EventHandler TraceEventHandler;

    public XmlPanelRepository()
    {
        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    }

    void panelCom_Trace(string message)
    {
        if (EventHandler != null)
          EventHandler("Event was hit, here's the message:", message);
    }
}

public interface IXmlConfigurationService
{
    //irrelevant stuff removed
}

public class XmlConfigurationService : IXmlConfigurationService
{
    public XmlConfigurationService(IXmlRepository configurationRepository)
    {
        _configurationRepository.TraceEventHandler += ConfigurationRepository_EventHandler;
    }

    void ConfigurationRepository_EventHandler(string parameter1, string parameter2);)
    {
        // Do something with your parameters.
        Response.Write(parameter1 + parameter2);
    }
}
于 2012-04-26T12:10:07.577 回答