在基类和子类的情况下,我需要定义INotifyPropertyChanged
本文关键字:定义 INotifyPropertyChanged 情况下 基类 子类 | 更新日期: 2023-09-27 18:22:29
我有这个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
。
如果您的xaml绑定到WiresharkFile的属性,那么WiresharkFile必须实现INotifyPropertyChanged,否则将导致内存泄漏(WPF编程的三大内存泄漏陷阱)。如果绑定仅在Libpcap类上定义,那么Libpcap必须实现INotifyPropertyChanged接口。在我的项目中,我创建了INotifyPropertyChanged接口的基本实现,然后每个基本模型和基本视图模型都继承自该实现。以下是一些基本代码:1.基础实施:
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.你的型号(在我看来):
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();
}
}
}
问候,
与IIan的答案相同,但适用于C#8和.Net Framework 4.8。
1.基本模型
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
{
string 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.您的型号
public class Current : ObservableObject
{
private string _status;
public Current()
{
Status = "Not Connected";
}
public string Status
{
get { return _status; }
set
{
_status = value;
OnPropertyChanged(); // call this to update
}
}
}
3.如何使用
<Label Content="{Binding Status}"/>