C# 如何在C语言中正确触发事件
我试图在单击按钮时触发事件。我点击按钮,什么也没发生 问题是,在OnEventA中,我总是将EventA设置为null: 编辑: 基于Henk Holterman的链接,我还测试了该代码:C# 如何在C语言中正确触发事件,c#,C#,我试图在单击按钮时触发事件。我点击按钮,什么也没发生 问题是,在OnEventA中,我总是将EventA设置为null: 编辑: 基于Henk Holterman的链接,我还测试了该代码: public delegate void Eventhandler(object sender, Eventargs args); // your publishing class class Foo { public event EventHandler Changed; // th
public delegate void Eventhandler(object sender, Eventargs args);
// your publishing class
class Foo
{
public event EventHandler Changed; // the Event
protected virtual void OnChanged() // the Trigger method, called to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = Changed;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, EventArgs.Empty);
}
}
// an example of raising the event
void SomeMethod()
{
if (...) // on some condition
OnChanged(); // raise the event
}
}
我在单击按钮时调用OnChanged,但结果始终不变:EventA=null。我通过以下方式解决了问题:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace eventsC
{
public partial class Form1 : Form
{
public static event EventHandler<EventArgs> myEvent;
protected void OnMyEvent()
{
if (myEvent != null)
myEvent(this, EventArgs.Empty);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myEvent += Handler;
//call all methods that have been added to the event
myEvent(this, EventArgs.Empty);
}
private void button1_Click(object sender, EventArgs e)
{
OnMyEvent();
}
static void Handler(object sender, EventArgs args)
{
Console.WriteLine("Event Handled!");
}
}
}
我认为他们的回答是正确的,它没有任何订户
namespace TestConsoleApp
{
public delegate void Eventhandler(object sender, EventArgs args);
// your publishing class
class Foo
{
public event EventHandler Changed; // the Event
protected virtual void OnChanged() // the Trigger method, called to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = Changed;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, EventArgs.Empty);
}
}
// an example of raising the event
public void SomeMethod()
{
OnChanged(); // raise the event
}
}
public class Program
{
static void Main(string[] args)
{
Foo objFoo = new Foo();
objFoo.Changed += ObjFoo_Changed;
objFoo.SomeMethod();
Console.ReadLine();
}
private static void ObjFoo_Changed(object sender, EventArgs e)
{
Console.WriteLine("Event fired and changed");
}
}
}
您的活动有订户吗?我在@PeterBons上写了完整的图片:我需要在同一个解决方案中的另一个VB.NET项目中接收该活动。但由于EventA=null而从未到达。我可以像我展示的代码一样从同一个类触发事件吗?您没有提供足够的信息来回答问题-您的+=?Re行在哪里?如果我没有,那么您没有subcriber,在这种情况下,EventA应该为null。这似乎不能回答您的问题。您的问题似乎是关于使用不同班级的活动。是这样吗?重写OnMyEvent并在自己的类中处理事件是没有意义的。事件处理程序应该在订阅类中完成,而不是在Form1中
namespace TestConsoleApp
{
public delegate void Eventhandler(object sender, EventArgs args);
// your publishing class
class Foo
{
public event EventHandler Changed; // the Event
protected virtual void OnChanged() // the Trigger method, called to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = Changed;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, EventArgs.Empty);
}
}
// an example of raising the event
public void SomeMethod()
{
OnChanged(); // raise the event
}
}
public class Program
{
static void Main(string[] args)
{
Foo objFoo = new Foo();
objFoo.Changed += ObjFoo_Changed;
objFoo.SomeMethod();
Console.ReadLine();
}
private static void ObjFoo_Changed(object sender, EventArgs e)
{
Console.WriteLine("Event fired and changed");
}
}
}