【发布时间】:2018-04-26 18:34:24
【问题描述】:
我在设计时收到错误消息
必须完全分配PropertyChanged
在 ctor 中。如果不是 ctor 则不是错误消息。
如何解决这个问题?
public struct LogCurve : INotifyPropertyChanged
{
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void Notify()
{
OnPropertyChanged(string.Empty);
}
public string Name { get; }
public List<LogCurveDataPoint> LogPoints { get; }
public int ServerCount { get; }
public int Count { get { return LogPoints.Count; } }
public double? MinValue
{
get
{
return LogPoints.Count == 0 ? (double?)null : LogPoints.Min(x => x.Value);
}
}
public double? MaxValue
{
get
{
return LogPoints.Count == 0 ? (double?)null : LogPoints.Max(x => x.Value);
}
}
public long? MinIndex
{
get
{
return LogPoints.Count == 0 ? (long?)null : LogPoints.Min(x => x.Index);
}
}
public long? MaxIndex
{
get
{
return LogPoints.Count == 0 ? (long?)null : LogPoints.Max(x => x.Index);
}
}
public LogCurve(string name, int serverCount)
{
Name = name;
LogPoints = new List<LogCurveDataPoint>();
ServerCount = serverCount;
}
}
【问题讨论】:
-
不是您问题的答案,但要小心:在值类型上实现
INotifyPropertyChanged几乎肯定是个坏主意。值类型可能会被复制到您意想不到的地方,从而很容易订阅错误副本的更改。 -
@hvd 我想我要转课了。
-
事实上,每当您将结构作为参数提供给方法时,它都会被复制。因此,当您更改结构属性值时,仅通知副本,而不是您的原始参考。