【问题标题】:Unfamiliar operator in C# += and EventHandlersC# += 和 EventHandlers 中不熟悉的运算符
【发布时间】:2016-09-22 11:30:01
【问题描述】:

+= 运算符有什么作用?很难在网上找到有关此运营商的信息,尽管这似乎表明它与代表有关。谁能解释一下?

反正下面是我遇到的c#语句:

this.LineSelected += new LineSelectionHandler(AdvancedReceiptViewModel_LineSelected);

【问题讨论】:

  • 所以你知道它是一个运算符,所以为什么不从搜索C# operators 开始 - 第一个结果将指向你here:The += operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. The use of the += operator in this context is referred to as subscribing to an event.
  • 您可能不熟悉 VB.NET,但如果您熟悉的话,它相当于 AddHandler 语句。

标签: c# event-handling


【解决方案1】:

它为该事件添加一个 EventHandler。

例如:

Button.Click += new EventHandler(my_handler)

Click 是您希望处理或收听的事件,my_handler 是在该事件发生时将调用的方法。

【讨论】:

    【解决方案2】:

    对于事件处理程序,+= 具有订阅语义或 -= 取消订阅 https://msdn.microsoft.com/en-us/library/ms366768.aspx

    【讨论】:

      【解决方案3】:

      += 是附加运算符。这意味着右侧组件与左侧组件集成在一起。 x+=y - x=x+y button.Click += new event() - 按钮的点击事件进程启用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2010-09-14
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        相关资源
        最近更新 更多