如何在 C# Web 窗体上使用事件委托?

How to use event delegate on C# web form?

我有两个 .cs 文件。我正在尝试在此项目中使用事件委托。 Event.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project3
{
    public delegate int Calculate(int obj1, int obj2);
    public class Event
    {
        public event Calculate CalculateNow;
        int a = 0;
        int b = 0;
        int r = 0; 
        public int Add()
        {
            r = a + b;
            return r; 
        }

        public int Sub()
        {
            r = a - b;
            return r; 
        }

        public int Mul()
        {
            r = a * b;
            return r; 

        }

        public int Div()
        {
            r = a / b;
            return r; 
        }
    }
}

和Form1.cs:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            label1.Text = btn.Text;
            int num1 = Int32.Parse(textBox1.Text);
            int num2 = Int32.Parse(textBox2.Text);
            Event t = new Event();
            t.CalculateNow +=new Calculate(t_CalculateNow);
            int finalr = t.Add();
            if (finalr != null)
            {
                label1.Text = "" + finalr;
            }
            else
            {
                label1.Text = "Error!";
            }
        }
    }
}

我收到一个错误 "The name 't_CalculateNow' does not exist in the current context",我不知道如何更正它。希望有人能回答我。太感谢了!如果有人能解释一下事件委托的逻辑,那将对我有很大帮助。

您的代码缺少正确的事件初始化和使用。不能从其他 class 直接调用事件,但是可以创建为 public 订阅者调用它的方法(在我们的例子中是 InvokeCalc 方法)。看起来像这样的东西对你有用:

public class Calculator
{
    public event Calculate CalculateNow;
    public delegate int Calculate(int x, int y);

    public string InvokeCalc(int x, int y)
    {
        if(CalculateNow != null) return CalculateNow(x, y).ToString();
        return null;
    }
}

然后在你的 winform 中你可以使用它:

namespace Project3
{
    public partial class Form1 : Form
    {
        private Calculator calc;

        public Form1()
        {
            InitializeComponent();
            calc = new Calculator();
            label1.Text = "";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            label1.Text = btn.Text;
            int num1 = Int32.Parse(textBox1.Text);
            int num2 = Int32.Parse(textBox2.Text);

            // subscribes a handler to your event
            calc.CalculateNow += (n1, n2) => { return n1 + n2; }

            // method that invokes your event on Calculator class
            int finalr = calc.InvokeCalc(num1, num2);
            if (finalr != null)
            {
                label1.Text = "" + finalr;
            }
            else
            {
                label1.Text = "Error!";
            }
        }
    }
}

P.S。 虽然这是一个说明如何使用事件的示例,但您不太可能必须以这种方式使用事件。通常,事件应该用于通知订阅者(其他 classes)事件已经发生。事件只能从 class 内部触发,这种某种封装提高了应用程序的完整性,否则 ANY class 或结构可以触发此事件,并且很难找出哪个 class 实际调用了它以及在哪里...