C# 事件未被事件使用

C# 事件未被事件使用,c#,.net,events,C#,.net,Events,我试图理解C#中的募捐和消费活动 我无法使用我提出的事件 你能看一下我的密码吗?谢谢 class Program { static void Main(string[] args) { var evtClass = new EventClass(); evtClass.OnVariableLoaded(new EventClass.CustomEventArgs("test"));ded; } static void c_Vari

我试图理解C#中的募捐和消费活动

我无法使用我提出的事件

你能看一下我的密码吗?谢谢

class Program
{
    static void Main(string[] args)
    {
        var evtClass = new EventClass();
        evtClass.OnVariableLoaded(new EventClass.CustomEventArgs("test"));ded;
    }

    static void c_VariableLoaded(object sender, EventClass.CustomEventArgs e)
    {
        // The event is not being executed...
    }
}
public class EventClass
{
    public event EventHandler<CustomEventArgs> VariableLoaded;

    protected virtual void OnVariableLoaded(CustomEventArgs eventArgs)
    {
        VariableLoaded?.Invoke(this, eventArgs);
    }

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string variable1)
        {
            Variable1 = variable1;
        }

        public string Variable1 { get; }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var evtClass=new EventClass();
evtClass.OnVariableLoaded(新的EventClass.CustomEventArgs(“测试”);ded;
}
静态void c_VariableLoaded(对象发送方,EventClass.CustomEventArgs e)
{
//事件未被执行。。。
}
}
公共类事件类
{
公共事件EventHandler VariableLoaded;
受保护的虚拟void OnVariableLoaded(CustomEventArgs eventArgs)
{
VariableLoaded?.Invoke(此为eventArgs);
}
公共类CustomEventArgs:EventArgs
{
公共CustomEventArgs(字符串变量1)
{
Variable1=Variable1;
}
公共字符串变量1{get;}
}
}

在您的代码中,您只需使用

evtClass.VariableLoaded += c_VariableLoaded;
但这一事件并没有发生。必须有东西从evtClass调用它,才能使main中的事件处理程序正常工作

例如,考虑查看Windows窗体事件。您正在使用一些事件处理程序订阅Button Click事件,就像在main中一样。但必须按下按钮才能运行处理程序的代码

所以它在您的代码中—您订阅了事件,但事件本身从未引发。要从代码中提升它,应该使用您构造的一些参数调用
onvariableloadded


考虑寻找一些事件示例,例如,进度条更新或PropertyChanged模式。

没有人调用OnVariableLoaded?@Luca我如何调用OnVariableLoaded?我已经更新了代码,但仍然不知道如何使用事件。