我需要订阅列表中对象的属性。我找到了这个示例(https://stackoverflow.com/a/18770397/3954928),它可以完美地工作,但是如果我向列表中添加新元素,它将无法正常工作。有什么办法吗?谢谢!
编辑
IDisposable subscription =
Observable
.FromEventPattern
<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
x => MyList.CollectionChanged += x,
x => MyList.CollectionChanged -= x)
.Where(x => x.EventArgs.Action == NotifyCollectionChangedAction.Add)
.SelectMany(x => x.EventArgs.NewItems.Cast<MyCustomClass>())
.SelectMany(x =>
{
CallMethodWhenAddItem(x);
return x.OnPropertyChange(nameof(x.MyCustomProperty));
})
.Subscribe(x =>
// x is PropertyChangedEventArgs, not MyCustomClass
if (x.MyCustomProperty == "SomeValue") {
RunAction();
}
});
public static IObservable<PropertyChangedEventArgs> OnPropertyChange<T>(this T currentSource, string propertyName)
where T : INotifyPropertyChanged
{
return
Observable
.FromEventPattern
<PropertyChangedEventHandler, PropertyChangedEventArgs>(
eventHandler => eventHandler.Invoke,
x => currentSource.PropertyChanged += x,
x => currentSource.PropertyChanged -= x)
.Where(x => x.EventArgs.PropertyName == propertyName)
.Select(x => x.EventArgs);
}
您能指导我以下几个问题吗?
1)使用“ eventHandler => eventHandler.Invoke”和不使用它有什么区别?互联网上的许多示例都使用它,而其他示例则没有。而且我真的看不出有什么区别。
2)如何取消订阅“动态添加”的属性。只需将其从列表中删除?
谢谢!
答案 0 :(得分:1)
System.Func<EventHandler<TEventArgs>, TDelegate>
参数用于将给定的事件处理程序转换为与基础.NET事件兼容的委托。如果不需要-请勿使用。它确实用于连接非标准事件处理程序。
您绝对不应进行嵌套订阅。这样,您就不必担心处理内部订阅。这样编写查询:
IDisposable subscription =
Observable
.FromEventPattern
<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
x => MyList.CollectionChanged += x,
x => MyList.CollectionChanged -= x)
.Where(x => x.EventArgs.Action == NotifyCollectionChangedAction.Add)
.SelectMany(x => x.EventArgs.NewItems.Cast<MyCustomClass>())
.SelectMany(x =>
{
CallMethodWhenAddItem(x);
return x.OnPropertyChange(nameof(x.MyCustomProperty));
})
.Subscribe(_ => { });
根据评论进行了更新:
void Main()
{
var MyList = new ObservableCollection<int>();
IDisposable subscription =
Observable
.FromEventPattern
<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
x => MyList.CollectionChanged += x,
x => MyList.CollectionChanged -= x)
.Where(x => x.EventArgs.Action == NotifyCollectionChangedAction.Add)
.SelectMany(x => x.EventArgs.NewItems.Cast<MyCustomClass>())
.SelectMany(x =>
{
CallMethodWhenAddItem(x);
return x.OnPropertyChange(nameof(x.MyCustomProperty));
})
.Where(x => x.MyCustomProperty == "SomeValue")
.Subscribe(_ => { });
}
public static class Ex
{
public static IObservable<T> OnPropertyChange<T>(this T currentSource, string propertyName)
where T : INotifyPropertyChanged
{
return
Observable
.FromEventPattern
<PropertyChangedEventHandler, PropertyChangedEventArgs>(
eventHandler => eventHandler.Invoke,
x => currentSource.PropertyChanged += x,
x => currentSource.PropertyChanged -= x)
.Where(x => x.EventArgs.PropertyName == propertyName)
.Select(x => currentSource);
}
}
public class MyCustomClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string MyCustomProperty { get; set; }
}
public void CallMethodWhenAddItem(MyCustomClass x) { }