C# InotifyProperty更改时的复合模型绑定性能
我目前正在开发一个解决方案,它有一组复合视图模型,这些模型是从一组数据访问服务返回的域模型映射而来的 到目前为止,我已经在基本ViewModel对象上实现了C# InotifyProperty更改时的复合模型绑定性能,c#,data-binding,inotifypropertychanged,wpf-4.0,C#,Data Binding,Inotifypropertychanged,Wpf 4.0,我目前正在开发一个解决方案,它有一组复合视图模型,这些模型是从一组数据访问服务返回的域模型映射而来的 到目前为止,我已经在基本ViewModel对象上实现了INotifyPropertyChanged,并通过属性更改事件通知UI属性对象的更改,取得了很大的成功 以下是视图模型的示例: public class DisplayDataModel : INotifyPropertyChanged{ private DateTime _lastRefreshTime; public D
INotifyPropertyChanged
,并通过属性更改事件通知UI属性对象的更改,取得了很大的成功
以下是视图模型的示例:
public class DisplayDataModel : INotifyPropertyChanged{
private DateTime _lastRefreshTime;
public DateTime LastRefreshTime {
get { return _lastRefreshTime; }
set {
_lastRefreshTime = value;
this.NotifyPropertyChanged(lddm => lddm.LastRefreshTime, PropertyChanged);
}
}
private string _lineStatus;
public string LineStatus {
get { return _lineStatus; }
set {
if (_lineStatus != value) {
_lineStatus = value;
this.NotifyPropertyChanged(lddm => lddm.LineStatus, PropertyChanged);
}
}
}
private ProductionBrickModel _productionBrick;
public ProductionBrickModel ProductionBrick {
get { return _productionBrick;}
set {
if (_productionBrick != value) {
_productionBrick = value;
this.NotifyPropertyChanged(lddm => lddm.ProductionBrick, PropertyChanged);
}
}
}
}
public class ProductionBrickModel{
public int? Set { get; set; }
public int? Theoretical { get; set; }
public int? Actual { get; set; }
public string LineName { get; set; }
public TimeSpan? ShiftOverage { get; set; }
public SolidColorBrush ShiftOverageBrush {
get {
if (ShiftOverage.HasValue && ShiftOverage.Value.Milliseconds < 0) {
return Application.Current.FindResource("IndicatorRedBrush") as SolidColorBrush;
}
return Application.Current.FindResource("IndicatorWhiteBrush") as SolidColorBrush;
}
}
public string ShiftOverageString { get { return ShiftOverage.HasValue ? ShiftOverage.Value.ToShortTimeSpanString() : ""; } }
}
公共类DisplayDataModel:INotifyPropertyChanged{
私有日期时间\u lastRefreshTime;
公共日期时间最后刷新时间{
获取{return\u lastfreshttime;}
设置{
_lastRefreshTime=值;
this.NotifyPropertyChanged(lddm=>lddm.lastfreshtTime,PropertyChanged);
}
}
私有字符串_lineStatus;
公共字符串行状态{
获取{return\u lineStatus;}
设置{
如果(_lineStatus!=值){
_lineStatus=值;
this.NotifyPropertyChanged(lddm=>lddm.LineStatus,PropertyChanged);
}
}
}
私人生产砖模型(productionBrick),;
公共生产砖块模型生产砖块{
获取{return\u productionBrick;}
设置{
如果(_productionBrick!=值){
_productionBrick=价值;
this.NotifyPropertyChanged(lddm=>lddm.ProductionBrick,PropertyChanged);
}
}
}
}
公共类ProductionBrickModel{
公共int?Set{get;Set;}
公共整数?理论{get;set;}
公共int?实际值{get;set;}
公共字符串LineName{get;set;}
公共时间跨度?shiftoAverage{get;set;}
public-SolidColorBrush转换为verageBrush{
得到{
if(ShiftOverage.HasValue&&ShiftOverage.Value.millides<0){
将Application.Current.FindResource(“IndicatorRedBrush”)返回为SolidColorBrush;
}
将Application.Current.FindResource(“IndicatorWhiteBrush”)返回为SolidColorBrush;
}
}
公共字符串ShiftOverageString{get{return ShiftOverage.HasValue?ShiftOverage.Value.ToSortTimeSpansString():“”;}
}
因此,目前我在基本模型上触发通知事件,而不是在生产砖属性上,这主要是因为生产砖属性几乎在每次刷新时都会发生更改
最近,我开始将刷新时间调低到350ms左右,我看到ShiftOverageBrush
在瞬间变为白色,即使值仍然为负值
我的问题是,通过在组成基本视图模型的对象类型上执行并实现
INotifyPropertyChanged
,我会获得任何性能,甚至可能解决此问题吗?或者这完全是我不理解的其他原因造成的?代码中有两个明显的低效原因:
1) ShiftOverageBrush每次调用时都使用FindResource。为什么不缓存笔刷呢
private SolidColorBrush _redBrush;
private SolidColorBrush IndicatorRedBrush
{
get{ return _redBrush ?? (_redBrush =
Application.Current.FindResource("IndicatorRedBrush") as SolidColorBrush));
}
... same for white brush
public SolidColorBrush ShiftOverageBrush {
get {
if (ShiftOverage.HasValue && ShiftOverage.Value.Milliseconds < 0) {
return IndicatorRedBrush;
}
return IndicatorWhiteBrush;
}
}
private SolidColorBrush\u redBrush;
专用SolidColorBrush指示灯
{
获取{return\u redBrush???(\u redBrush=
Application.Current.FindResource(“IndicatorRedBrush”)作为SolidColorBrush);
}
…白色刷子也一样
public-SolidColorBrush转换为verageBrush{
得到{
if(ShiftOverage.HasValue&&ShiftOverage.Value.millides<0){
返回指示灯指示灯;
}
返回指示灯闪烁;
}
}
2) 为NotifyPropertyChanged使用lambda表达式很方便,但速度很慢,因为它使用反射。如果您正在加快更新速度,请使用字符串替换lambda。好建议。笔刷在不同的模型中被广泛使用,因此我想我将创建一个笔刷缓存类来处理此问题。这样我就可以拉必要时从那里刷牙。