学习011-05 The Importance of Property Change Notifications for Automatic UI Updates(属性更改通知对自动UI更新的重要性)

The Importance of Property Change Notifications for Automatic UI Updates(属性更改通知对自动UI更新的重要性)

To provide a consistent user interface, your XAF application must receive notifications from business classes when their property values are changed. For instance, Conditional Appearance and Validation modules may require an immediate UI update when a user modifies a certain value. This topic describes how to enable notifications from XPO, Entity Framework Core, and non-persistent business classes.
为了提供一致的用户交互界面,您的XAF应用程序必须在业务类的属性值更改时接收来自业务类的通知。例如,当用户修改某个值时,条件外观和验证模块可能需要立即更新UI。本主题介绍如何启用来自XPO、实体框架核心和非持久性业务类的通知。

The notification mechanism is based on supporting the standard INotifyPropertyChanged interface and implementing its declared PropertyChanged event. To send a notification to internal XAF code, trigger PropertyChanged from a property set accessor within a business class.
通知机制基于支持标准INotifyPropertyChanged接口并实现其声明的属性更改事件。要向内部XAF代码发送通知,请从业务类中的属性集访问器触发属性更改。

PropertyChanged Event in XPO(XPO中的属性更改事件)

You do not need to implement the INotifyPropertyChanged interface manually in an XPO business class. When you declare an XPO business class, you inherit BaseObject (or another base persistent class) that already supports this interface. You can trigger the PropertyChanged event from a property set accessor by executing the XPBaseObject.OnChanged or PersistentBase.SetPropertyValue helper method.
您不需要在XPO业务类中手动实现INotifyPropertyChanged接口。当您声明XPO业务类时,您继承了已经支持此接口的BaseObject(或其他基本持久类)。您可以通过执行XPBaseObject. OnChanged或PersitentBase.SetProperties tyValue帮助方法从属性集访问器触发属性更改事件。

C#
public class Department : BaseObject {
    // ...
    private string title;
    public string Title {
        get {
            return title;
        }
        set {
            SetPropertyValue(nameof(Title), ref title, value);
        }
    }
}

PropertyChanged Event in Entity Framework Core(实体框架核心中的属性更改事件)

XAF EF Core projects enable change-tracking proxies with the UseChangeTrackingProxies method. To support notifications in your business classes, define properties as described in the following article: Change-tracking proxies:
XAF EF Core项目使用UseChangeTrackingProxies方法启用更改跟踪代理。要支持业务类中的通知,请按照以下文章中的说明定义属性:更改跟踪代理:

C#
public class Blog {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Post> Posts { get; } = new ObservableCollection<Post>();
}

Important
In EF Core-based projects, the XAF Security System requires that Change Tracking is enabled when you work with DbContext.
在基于EF Core的项目中,XAF安全系统要求在您使用DbContext时启用更改跟踪。
If LINQ Expressions call the AsNoTracking() or AsNoTrackingWithIdentityResolution() method, or if Change Tracking is otherwise disabled, a data request becomes insecure. This may cause data to be retrieved from a database prohibited by the Security System.
如果LINQ表达式调用AsNoTrack()或AsNoTrackingWithIdentityResolution()方法,或者禁用更改跟踪,则数据请求变得不安全。这可能会导致从安全系统禁止的数据库中检索数据。

PropertyChanged Event in Non-Persistent Classes(非持久性类中的属性更改事件)

There are two ways to use the PropertyChanged event in a non-persistent class:
有两种方法可以在非持久性类中使用Properties tyChanged事件:

Inherit your non-persistent class from base non-persistent classes, as described in Non-Persistent Objects.
从基非持久类继承您的非持久类,如非持久对象中所述。

Implement this interface using the approach described above for the Entity Framework.
使用上面为实体框架描述的方法实现此接口。

C#
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;
}

To create a new non-persistent class that supports INotifyPropertyChanged, use the XAF Business Object | Non-Persistent Object project item template.
要创建支持INotifyPropertyChanged的新非持久性类,请使用XAF业务对象|非持久性对象项目项模板。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值