0

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

到目前为止,我INotifyPropertyChanged在基础 ViewModel 对象上实现并通过属性更改事件通知 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对构成基本视图模型的对象类型进行检查和实现,我会获得任何性能,甚至可能解决这个问题吗?或者这完全来自我不理解的其他东西?

4

1 回答 1

2

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

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。

于 2013-03-21T17:38:32.507 回答