[英]Why can't I raise or call event if I have overloaded add and remove in C#
While copying code for RelayCommand from Josh Smith article I copied following code 在从Josh Smith文章中复制RelayCommand的代码时,我复制了以下代码
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
Then after reading this answer on SO I also copied in my class following code from DelegateCommand class of Prism. 然后在阅读了这个关于SO的答案后,我还在我的课程中复制了来自Prism的DelegateCommand类的代码。
protected void NotifyCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
But his gives me an error in NotifyCanExecuteChanged method 但是他在NotifyCanExecuteChanged方法中给了我一个错误
The event 'CanExecuteChanged' can only appear on the left hand side of += or -=
事件'CanExecuteChanged'只能出现在+ =或 - =的左侧
This error is not coming if I remove the add and remove overload from event. 如果我从事件中删除添加和删除重载,则不会出现此错误。 Can someone please help me understand the reason behind this?
有人可以帮我理解这背后的原因吗?
With a field-like event (which is the name for the simple form without add
/ remove
, then when you do if(CanExecuteChanged != null)
or CanExecuteChanged(this, ...)
, the CanExecuteChanged
is referring to the backing field , which is a delegate field of type EventHandler
. You can invoke a delegate field. However, that is not the case in your example, because there is no obvious thing to invoke. There certainly isn't a local field, and the forwarded event ( CommandManaged.RequerySuggested
) does not intrinsically expose any "invoke" capability. 使用类似字段的事件(这是没有
add
/ remove
的简单表单的名称,然后当你执行if(CanExecuteChanged != null)
或CanExecuteChanged(this, ...)
, CanExecuteChanged
指的是支持字段 ,这是一个EventHandler
类型的委托字段。你可以调用一个委托字段。但是,在你的例子中并非如此,因为没有明显的事情可以调用。当然没有本地字段和转发事件( CommandManaged.RequerySuggested
) 本质上不暴露任何“调用”功能。
Basically, for that to work you would need access to an invoke mechanism. 基本上,要使其工作,您需要访问调用机制。 Most commonly, I would expect that to take the form of:
最常见的是,我希望采取以下形式:
CommandManager.OnRequerySuggested();
but if there is a method that invokes this event (and there doesn't need to be), it could be called anything . 但如果有一个方法调用此事件(并且不需要),则可以调用任何方法 。
(the On*
is a common pattern for a "raise this event" API, doubly-so if it is polymorphic) (
On*
是“提升此事件”API的常见模式,如果它是多态的话,加倍 - 所以
I think you want CommandManager.InvalidateRequerySuggested . 我想你想要CommandManager.InvalidateRequerySuggested 。 It forces the
RequerySuggested
event to be raised. 它强制
RequerySuggested
事件。
It seems like your class inherits from the class where event is declared. 看起来你的类继承自声明事件的类。 Event can be directly raised only in the base class, not in inherited class.
事件只能在基类中直接引发,而不能在继承的类中引发。
If you want to raise it in inherited class, write the following method in your base and call it from inherited class: 如果要在继承的类中引发它,请在基类中编写以下方法并从继承的类中调用它:
protected void RaiseMyEvent()
{
if (MyEvent != null)
{
MuEvent(this, args)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.