代码之家  ›  专栏  ›  技术社区  ›  Alison

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

  •  1
  • Alison  · 技术社区  · 10 年前

    我有两个.cs文件。我正在尝试在这个项目中使用事件委托。事件.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!";
                }
            }
        }
    }
    

    我收到一个错误:“名称't_CalculateNow'在当前上下文中不存在”,我不知道如何更正。希望有人能回答我。非常感谢!如果有人能解释事件委托的逻辑,这将对我有很大帮助。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Fabjan    10 年前

    您的代码缺少正确的事件初始化和使用。事件不能直接从其他类调用,但可以创建将为公共订阅者调用它的方法(在本例中是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!";
                }
            }
        }
    }
    

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