商信互联
为了提供一致的用户界面,当您的XAF应用程序的属性值更改时,必须从业务类接收通知。例如,当用户修改某个值时,条件外观和验证模块可能需要立即更新UI。本主题描述如何提供来自XPO,实体框架和非持久性业务类的通知。
通知机制基于支持标准INotifyPropertyChanged接口并实现其声明的PropertyChanged事件。要将通知发送到内部XAF代码,请从业务类中的属性集访问器触发PropertyChanged。
无需在XPO业务类中手动实现INotifyPropertyChanged接口。声明XPO业务类时,您将继承已经支持此接口的BaseObject(或另一个基本持久性类)。您可以通过执行XPBaseObject.OnChanged或PersistentBase.SetPropertyValue帮助器方法,从属性集访问器触发PropertyChanged事件。
public class Department : BaseObject {
// ...
private string title;
public string Title {
get {
return title;
}
set {
SetPropertyValue(nameof(Title), ref title, value);
}
}
}
Public Class Department
Inherits BaseObject
' ...
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
SetPropertyValue(NameOf(Title), _title, value)
End Set
End Property
End Class
另请参见: 简化的属性语法
在实现EntityFramework业务类时,您不会继承任何标准基类。因此,您应该手动实现INotifyPropertyChanged接口。XAF提供XAF业务对象| EF Business Object 项目项模板可简化此任务。
在现有的类中,可以如下实现INotifyPropertyChanged。
public class Customer : INotifyPropertyChanged {
// ...
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Public Class Customer
Implements INotifyPropertyChanged
' ...
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged( _
<System.Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = Nothing)
Dim args As New PropertyChangedEventArgs(propertyName)
RaiseEvent PropertyChanged(Me, args)
End Sub
End Class
您可以使用OnPropertyChanged帮助程序方法从属性集访问器触发PropertyChanged事件。
public class Customer : INotifyPropertyChanged {
// ...
private CustomerStatus status;
public CustomerStatus Status {
get { return status; }
set {
if(status != value) {
status = value;
OnPropertyChanged();
}
}
}
}
Public Class Customer
Implements INotifyPropertyChanged
' ...
Private _status As CustomerStatus
Public Property Status() As CustomerStatus
Get
Return _status
End Get
Set(ByVal value As CustomerStatus)
If _status IsNot value Then
_status = value
OnPropertyChanged()
End If
End Set
End Property
End Class
要在非持久类中使用PropertyChanged事件,请遵循上面针对实体框架描述的相同方法。
public class NonPersistentObject1 : INotifyPropertyChanged {
// ...
private string sampleProperty;
public string SampleProperty {
get { return sampleProperty; }
set {
if (sampleProperty != value) {
sampleProperty = value;
OnPropertyChanged();
}
}
}
private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Public Class NonPersistentObject1
Implements INotifyPropertyChanged
' ...
Private _sampleProperty As String
Public Property SampleProperty() As String
Get
Return _sampleProperty
End Get
Set(ByVal value As String)
If _sampleProperty <> value Then
_sampleProperty = value
OnPropertyChanged()
End If
End Set
End Property
Private Sub OnPropertyChanged( _
<System.Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
要创建支持INotifyPropertyChanged的新的非持久类,请使用XAF Business Object | XAF。非持久对象 项目项目模板。