【问题标题】:Composite Model Binding Performance with INotifyPropertyChanged使用 INotifyPropertyChanged 的​​复合模型绑定性能
【发布时间】:2013-03-21 17:34:26
【问题描述】:

我目前正在研究一种解决方案,该解决方案具有一组复合 ViewModel,这些 ViewModel 映射自从一组数据访问服务返回的域模型。

到目前为止,我在基本 ViewModel 对象上实现 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() : ""; } }

}

所以目前我在基础模型而不是生产砖属性上触发通知事件,主要是因为生产砖属性几乎每次刷新都会改变。

最近我开始将刷新时间缩短到 350 毫秒左右,我看到 ShiftOverageBrush 在瞬间变为白色的情况,即使值仍然是负数。

我的问题是,通过在构成基本视图模型的对象类型上执行 INotifyPropertyChanged 并实现任何性能,甚至可能解决这个问题吗?或者这完全来自其他我不理解的东西?

【问题讨论】:

    标签: c# data-binding inotifypropertychanged wpf-4.0


    【解决方案1】:

    您的代码中有两个明显的低效率来源:

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

    2) 对 NotifyPropertyChanged 使用 lambda 表达式很方便,但由于它使用反射,所以速度很慢。如果您要提高更新率,请用字符串替换 lambda。

    【讨论】:

    • 好建议。画笔在不同的模型中被广泛使用,所以我想我会制作一个画笔缓存类来处理它。这样我可以在必要时从那里拉刷子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多