【问题标题】:Parameter in event handler is not hitting while debugging调试时事件处理程序中的参数未命中
【发布时间】:2018-02-20 16:35:25
【问题描述】:

我有一个如下代码的事件处理程序:

viewer.LocalReport.SubreportProcessing += new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

并且这个方法作为上面事件处理程序的参数:

private static void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) {

        DateTime movementDate = Convert.ToDateTime(e.Parameters[0].Values[0]);

        TourTransactionsController controller = new TourTransactionsController();

        var movement = controller.Movements();

        List<Movement> movementList = new List<Movement>();
        movementList.Add(new Movement {
            Destination = "TEST",
            MovementDescription = "TEST",
            DateTime = Convert.ToDateTime("2017-09-25")
        });

        e.DataSources.Clear();

        e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource() {
            Name = "DSMovements",
            Value = movementList
        });

        //throw new NotImplementedException();
    }

这两种方法都写在 WEB API 控制器中。事件处理程序在调试时命中,但在我在调试 LocalReport_SubreportProcessing 方法时按 F11(步入)后没有命中。为什么LocalReport_SubreportProcessing 方法没有命中?

非常感谢任何帮助或回答。

【问题讨论】:

  • 您只有 += / add 事件处理程序。你确定事件是从LocalReport 触发的吗?
  • 我认为因为代码是在我点击 Web API 时执行的,所以事件是从 Web API 触发的。访问 web api 时是否可以触发事件处理程序?
  • 抱歉,很难理解您想要什么。 The eventhandler is hitting while debugging, but after I press F11 (step into) - 您是否希望在您 += / add 时调用事件处理程序?
  • 是的。我希望在我 += 时调用事件处理程序。老实说,我不确定我是否了解事件处理程序的工作原理。我的理解是当我 += / 添加它时调用事件处理程序正确吗?

标签: c# asp.net api asp.net-web-api web


【解决方案1】:

注册时不会调用事件/add+=

事件在所属类调用时被调用。

public class EventTest
{
    void SomeOperation()
    {
        //Do something
    }

    public void Run()
    {
        SomeOperation();
        RunFinished?.Invoke(this, EventArgs.Empty); //Invoke the event, indicating that something has happened or finished
    }

    //The event itself
    public event EventHandler RunFinished;
}

public class EventSubscriber
{
    EventTest _ET = new EventTest();

    public EventSubscriber()
    {
        _ET.RunFinished += ETRunFinished; //Register my method, called when the event occurs (is invoked)
    }

    public void DoSomething()
    {
        _ET.Run();
        Console.WriteLine("Something completed.");
    }

    void ETRunFinished(object sender, EventArgs e)
    {
        Console.WriteLine("My event handler was executed.");
    }
}

事件触发时将调用您的处理程序。

当注册+=-= 取消注册(添加/删除)时,它们不会被调用。

更多detailed tutorial请参考MSDN。

【讨论】:

  • 啊,我明白了。但是我在代码中用作EventHandler 的是来自Microsoft.Reporting.WebformsSubreportProcessingEventHandler。我尝试用viewer.LocalReport.SubreportProcessing?.Invoke(LocalReport_SubreportProcessing) 调用EventHandler,但我收到一条错误消息The event 'LocalReport.SubreportProcessing' can only appear on the left hand side of += or -=There is no argument given that corresponds to the required formal parameter 'e' of 'SubreportProcessingEventHandler.Invoke(object, SubreportProcessingEventArgs)' 有没有其他方法可以调用EventHandler
  • @SatriaJanaka 您只能在声明类中调用event。从它派生也不会帮助你。 invoke it via reflection, 是可能的,但这肯定不是你想要的
  • @SatriaJanaka 如果你想调用/调用一个方法,就去做吧。在事件上注册它以在事件发生并且您想做某事时接收通知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多