我在屬性值計算中看到了非常奇怪的行爲。 我有一個屬性HasChanged,如果它的任何依賴屬性爲true,則爲true。 但是我得到了一個結果 - 所有參數都是錯誤的,結果是真的。 我使用MVVM燈框架,每個屬性INotifyPropertyChanged的Linq Any返回true,儘管集合中的所有值都是假的
下面是輔助功能
private static bool PropertyWrapper(bool value, [CallerMemberName] string callerName = "")
{
Logger.Debug($"[{callerName}: {value}]");
return value;
}
private static T PropertyWrapper<T>(Expression<Func<T>> property)
{
var compiled = property.Compile();
var result = (T)compiled.DynamicInvoke();
Logger.Debug($"[{GetName(property)}: {result}]");
return result;
}
private static string GetName<T>(Expression<Func<T>> expr)
{
var mexpr = expr.Body as MemberExpression;
if (mexpr == null) return "(null)";
if (mexpr.Member == null) return "((null))";
return mexpr.Member.Name;
}
這是代碼
public virtual bool HasChanged => PropertyWrapper(new[] {
PropertyWrapper(() => TitleChanged),
PropertyWrapper(() => EnglishTitleChanged),
PropertyWrapper(() => OriginalTitleChanged),
PropertyWrapper(() => PlotChanged),
PropertyWrapper(() => OutlineChanged),
PropertyWrapper(() => DirectorChanged),
PropertyWrapper(() => YearChanged),
PropertyWrapper(() => MovieRolesChanged),
PropertyWrapper(() => GenresChanged),
PropertyWrapper(() => CountriesChanged)
}.Any(), "HasChanged");
public bool YearChanged => this.state == State.Edit && this.source.MovieDescription.Year != this.clone.MovieDescription.Year;
public bool TitleChanged => HasTitleChanges();
public bool EnglishTitleChanged => HasEnglishTitleChanges();
public bool OriginalTitleChanged => HasOriginalTitleChanges();
public bool PlotChanged => HasDescriptionChanges();
public bool DirectorChanged => HasDirectorChanges();
public bool OutlineChanged => HasOutlineChanges();
public bool MovieRolesChanged => HasMovieRolesChanges();
public bool CountriesChanged => HasCountriesChanges();
public bool GenresChanged => HasGenresChanges();
,什麼是寫入日誌
[TitleChanged:False]
[EnglishTitleChanged:FALSE]
[OriginalTitleChanged:FALSE]
[PlotChanged:FALSE]
[OutlineChanged:FALSE]
[DirectorChanged:FALSE]
[YearChanged :False]
[MovieRolesChanged:False]
[GenresChanged:假]
[CountriesChanged:假]
[HasChanged:真]
它看起來不可能的枯燥,但我無法想象它怎麼可以。 請給我解釋這種行爲的原因。
總體正確,但元素是布爾,不是代表,所以它應該是'。任何(Q => Q) ' –
@IvanStoev,謝謝,編輯。我很困惑那裏有很多lambdas :) – slawekwin