我有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
。
答案 0 :(得分:2)
如果你的xaml绑定到WiresharkFile的属性,那么WiresharkFile必须实现INotifyPropertyChanged,否则会导致内存泄漏(Top 3 Memory Leak Inducing Pitfalls of WPF Programming)。如果仅在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();
}
}
}
的问候,
答案 1 :(得分:0)
与IIan的答案相同,但对于C#8和.Net Framework 4.8。
1。基本模型
position="sticky"
2。您的模型
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;
}
}
3。如何使用?
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
}
}
}