繁体   English   中英

C#触发事件

[英]C# trigger event

我使用C#,并且想从类中触发事件:

因此,如果更改了类的Price属性,则应触发onPriceChanged事件(在类外部)。 但是,我得到一个错误:

名称“ onPriceChanged”在当前上下文中不存在

我该如何解决? (我想我可以通过构造函数将事件处理程序传递给类……但如果可能的话,我宁愿不将事件处理程序传递给类)

这是我的代码:

using System;

public delegate void delEventHandler();

class clsItem
{
    //private static event delEventHandler _show;
    private delEventHandler _show;
    private int _price;

    public clsItem()  //Konstruktor
    {
        _show += new delEventHandler(Program.onPriceChanged);  //  error here :  The name 'onPriceChanged' does not exist in the current context
    }

    public int Price
    {
        set
        {
            _price = value;
            _show.Invoke();  //trigger Event when Price was changed
        }
    }
}

class Program
{
    static void Main()
    {
        clsItem myItem = new clsItem();
        myItem.Price = 123;   //this should trigger Event "onPriceChanged"
    }

    //EventHandler 
    public static void onPriceChanged()
    {
        Console.WriteLine("Price was changed");
    }
}

您正在以错误的方式执行此操作-您正在尝试从类中附加事件处理程序,很显然,该事件处理程序无法访问Program.onPriceChanged方法!

您应该公开事件,并从客户端代码( Program )附加事件处理Program

class clsItem
{
    //private static event delEventHandler _show;
    private delEventHandler _show;
    private int _price;

    public clsItem()  //Konstruktor
    {

    }

    public event delEventHandler Show
    {
        add { _show += value; }
        remove { _show -= value; }
    }

    public int Price
    {
        set
        {
            _price = value;
            _show?.Invoke();  //trigger Event when Price was changed
        }
    }
}

和:

clsItem myItem = new clsItem();
myItem.Show += onPriceChanged;
myItem.Price = 123;   //this now does trigger Event "onPriceChanged" 

实时示例: http//rextester.com/WMCQQ40264

这是您想要做的更传统的方式:

public delegate void delEventHandler();

class clsItem
{
    public event delEventHandler Show;
    private int _price;

    public clsItem()  //Konstruktor
    {
    }

    public int Price
    {
        set
        {
            _price = value;
            Show?.Invoke();  //trigger Event when Price was changed
        }
    }
}

class Program
{
    static void Main()
    {
        clsItem myItem = new clsItem();
        myItem.Show += onPriceChanged;
        myItem.Price = 123;   //this should trigger Event "onPriceChanged"
    }

    //EventHandler 
    public static void onPriceChanged()
    {
        Console.WriteLine("Price was changed");
    }
}

请注意, clsItem不再知道谁在订阅其事件。 它关心的只是通知恰好已订阅的所有侦听器。 clsItemonPriceChanged方法之间不再存在依赖关系。

您处理事件的方式不是一个好习惯。 我们使用事件的原因是将我们创建的对象与它们需要调用的方法分离。

例如,如果您要创建另一个具有相同类型(clsItem)的对象,并在价格改变后让它调用另一个方法,则会遇到麻烦。 因此,我建议使用此代码,而不是当前的代码:

using System;

public delegate void delEventHandler();

class clsItem
{
    public event delEventHandler PriceChanged;
    private int  _price;

    public clsItem()  //Konstruktor
    {

    }

    public int Price
    {
        set { 
              if(value!=_price) // Only trigger if the price is changed
              {
                _price = value;
                if(PriceChanged!=null) // Only run if the event is handled
                {
                    PriceChanged();
                }
              }
            }
    }   
}       

class Program
{   
    static void Main()
    {
       clsItem myItem = new clsItem();
       myItem.PriceChanged += new delEventHandler(onPriceChanged);
       myItem.Price = 123;   //this should trigger Event "PriceChanged" and call the onPriceChanged method
    }

    //EventHandler 
    public static void onPriceChanged()
    {
        Console.WriteLine("Price was changed");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM