财产事件 [英] event on a property

查看:93
本文介绍了财产事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个字符串类型的变量"card_serialNo";当值更改时,我想执行某些功能.请告知我是否可以对此类型的变量进行值更改事件.
谢谢.

Hi Everybody,
I have a string type variable ''card_serialNo"; I want to perform certain functions when it value get changed. please inform me if I can make a value change event for this type of variable.
thank You.

推荐答案

您可以为此变量创建一个属性并实现INotifyPropertyChanged接口,然后执行此操作. 否则,您可以为此变量创建一个属性,然后从该属性的set块中调用一个方法.
You can create a property for this variable and implement INotifyPropertyChanged interface and do this.
Or else, you can create a property for this variable and call a method from the set block of the property.


如果您仅在类内部需要此方法,则可以创建一个SetCardSerial方法或创建属性并使用设置器.

如果您想要在类之外可以访问的事件,则需要这样的内容:
If you just need this internally in the class, then either create a SetCardSerial method or create a property and use the setter.

If you want an event that is accessible outside the class then you will need something like this:
public class YourClass
{
    public event EventHandler CardSerialNumberChanged;

    private string cardSerialNo;

    public string CardSerialNo
    {
        get
        {
            if (cardSerialNo == null)
                return string.Empty;
            else
                return cardSerialNo;
        }
        set
        {
            if (value == string.Empty)
                value = null;
            if (cardSerialNo != value)
            {
                cardSerialNo = value;
                OnCardSerialNumberChanged(EventArgs.Empty);
            }
        }
    }

    protected virtual void OnCardSerialNumberChanged(EventArgs e)
    {
        EventHandler eh = CardSerialNumberChanged;
        if (eh != null) eh(this, e);
    }
}


这篇关于财产事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆