简体   繁体   English

如果我在C#中重载加载和删除,为什么我不能引发或调用事件#

[英]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.

相关问题 我可以在C#中调用同一个类的重载构造函数吗? - Can I call a overloaded constructor of the same class in C#? 我可以定义一个重载的'+'在C#中添加一个double和一个数组 - Can I define an overloaded '+' that add a double and an array in C# C# UWP 为什么我不能添加第二个事件处理程序? - C# UWP why can't I add a second event handler? 为什么我不能在画布 WPF C# 中删除图像? - Why I can't remove image in canvas WPF C#? 为什么我不能在 C# 中使用抽象静态方法? - Why can't I have abstract static methods in C#? 为什么不能在C#中用两个参数调用此构造函数? - Why can't I call this constructor with two arguments in C#? C#/ XNA为什么不能添加到阵列列表? - C#/XNA Why can't I add to the Array List? 为什么'='不会在C#中超载? - Why can '=' not be overloaded in C#? 无法在C#中引发UserControl的点击事件 - Can't raise click event of a UserControl in C# 在C#4中,如何在具有重载构造函数的父类的子类中具有带可选参数的构造函数? - In C# 4, how can I have constructors with optional parameters in a subclass of a parent with an overloaded constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM