INotifyPropertyChanged

INotifyPropertyChanged

本文介绍了在基类和子类的情况下需要定义INotifyPropertyChanged的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Base class:

public abstract class WiresharkFile
{
    protected string _fileName;
    protected int _packets;
    protected int _packetsSent;
    protected string _duration;

    public int Packets
    {
        get { return _packets; }
        set { _packets = value; }
    }

    public int PacketsSent
    {
        get { return _packetsSent; }
        set { _packetsSent = value; }
    }
}

这个子类:

public class Libpcap : WiresharkFile, IDisposable, IEnumerable<WiresharkFilePacket>
{
    ....
}

创建我的对象:

WiresharkFile wiresahrkFile = new Libpcap(file);

我的收藏集:

public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }

发送数据包:

wiresahrkFile.Sendpackets();

这时我所有的wiresahrkFile(Libpcap类型)属性都在变化,所以我想知道我需要在哪里定义此INotifyPropertyChanged.

At this point all my wiresahrkFile (Libpcap type) properties is changing so i wonder where i need to define this INotifyPropertyChanged.

推荐答案

如果您的xaml已绑定到WiresharkFile的属性,则WiresharkFile必须实现INotifyPropertyChanged,否则将导致内存泄漏(对WPF编程造成的前三项内存泄漏的陷阱).如果绑定仅在Libpcap类上定义,则Libpcap必须实现INotifyPropertyChanged接口.在我的项目中,我创建了INotifyPropertyChanged接口的基本实现,然后每个基本模型和基本视图模型都继承自该实现.下面是一些基本代码: 1.基本实现:

If your xaml is binded to properties of WiresharkFile then a WiresharkFile have to implement the INotifyPropertyChanged, if not it will lead to the memory leaks (Top 3 Memory Leak Inducing Pitfalls of WPF Programming). If your binding is defined only on a Libpcap class then the Libpcap have to implement the INotifyPropertyChanged interface. In my projects I create a base implementation of the INotifyPropertyChanged interface ,and then each base models and base view models just inherits from that implementation. Here some base code: 1. Base implementation:

public class BaseObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
    {
        var propName = ((MemberExpression)raiser.Body).Member.Name;
        OnPropertyChanged(propName);
    }

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(name);
            return true;
        }
        return false;
    }
}

2.您的模型(我认为):

2. Your model (in my opinion):

public abstract class WiresharkFile:BaseObservableObject
{
    private string _fileName;
    private int _packets;
    private int _packetsSent;
    private string _duration;

    public int Packets
    {
        get { return _packets; }
        set
        {
            _packets = value;
            OnPropertyChanged();
        }
    }

    public int PacketsSent
    {
        get { return _packetsSent; }
        set
        {
            _packetsSent = value;
            OnPropertyChanged();
        }
    }
}

致谢

这篇关于在基类和子类的情况下需要定义INotifyPropertyChanged的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:24