在 Base 和 sub 类 的情况下,我需要定义 INotifyPropertyChanged
Where i need to define INotifyPropertyChanged in case of Base and sub classes
我有这个 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; }
}
}
还有这个子 class:
public class Libpcap : WiresharkFile, IDisposable, IEnumerable<WiresharkFilePacket>
{
....
}
创建我的 object:
WiresharkFile wiresahrkFile = new Libpcap(file);
我的collection:
public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }
发送数据包:
wiresahrkFile.Sendpackets();
此时我的所有 wiresahrkFile
(Libpcap
类型)属性都在变化,所以我想知道我需要在哪里定义这个 INotifyPropertyChanged
。
如果您的 xaml 绑定到 WiresharkFile 的属性,则 WiresharkFile 必须实现 INotifyPropertyChanged,否则将导致内存泄漏 (Top 3 Memory Leak Inducing Pitfalls of WPF Programming)。如果您的绑定仅在 Libpcap class 上定义,则 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}"/>
我有这个 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; }
}
}
还有这个子 class:
public class Libpcap : WiresharkFile, IDisposable, IEnumerable<WiresharkFilePacket>
{
....
}
创建我的 object:
WiresharkFile wiresahrkFile = new Libpcap(file);
我的collection:
public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }
发送数据包:
wiresahrkFile.Sendpackets();
此时我的所有 wiresahrkFile
(Libpcap
类型)属性都在变化,所以我想知道我需要在哪里定义这个 INotifyPropertyChanged
。
如果您的 xaml 绑定到 WiresharkFile 的属性,则 WiresharkFile 必须实现 INotifyPropertyChanged,否则将导致内存泄漏 (Top 3 Memory Leak Inducing Pitfalls of WPF Programming)。如果您的绑定仅在 Libpcap class 上定义,则 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}"/>