1

我正在做的是这样的:

Item.PropertyChanged += (sender, args) =>
{
    if(sender is IInterface)
        DoSomethingWith(((IInterface)sender).PropertyFromInterface);
}

我将如何在 RxUI 中实现这样的流?

我试过这个:

this.WhenAny(x => (x.Item as IInterface).PropertyFromInterface, x.GetValue())
    .Subscribe(DoSomethingWith);

但似乎这是不可能的。

我必须做这样的财产吗?->

private IInterface ItemAsInterface { get { return Item as IInterface; } }

我现在做了一个解决方法,如下所示:

this.WhenAny(x => x.Item, x => x.GetValue()).OfType<IInterface>()
    .Select(x => x.PropertyFromInterface).DistinctUntilChanged()
    .Subscribe(DoSomethingWith);

但我真正想要的是在 Item 属于 IInterface 时为“PropertyFromInterface”获取 propertychanged 更新。

4

2 回答 2

1

怎么样:

this.WhenAny(x => x.Item, x => x.Value as IInterface)
    .Where(x => x != null)
    .Subscribe(DoSomethingWith);

更新:好的,我隐约明白你现在想做什么——我会这样做:

public ViewModelBase()
{
    // Once the object is set up, initialize it if it's an IInterface
    RxApp.MainThreadScheduler.Schedule(() => {
        var someInterface = this as IInterface;
        if (someInterface == null) return;

        DoSomethingWith(someInterface.PropertyFromInterface);
    });
}

如果你真的想通过 PropertyChanged 来初始化它:

this.Changed
    .Select(x => x.Sender as IInterface)
    .Where(x => x != null)
    .Take(1)   // Unsubs automatically once we've done a thing
    .Subscribe(x => DoSomethingWith(x.PropertyFromInterface));
于 2013-08-11T08:20:17.223 回答
0

回顾我的旧问题,我正在寻找类似这样的解决方案:

this.WhenAny(x => x.Item, x => x.GetValue()).OfType<IInterface>()
    .Select(x => x.WhenAny(y => y.PropertyFromInterface, y => y.Value).Switch()
    .Subscribe(DoSomethingWith);

对我来说缺少的链接是 .Switch 方法。

此外,如果属性不是所需的类型,我希望 observable 不做任何事情:

this.WhenAny(x => x.Item, x => x.Value as IInterface)
    .Select(x => x == null ? 
               Observable.Empty : 
               x.WhenAny(y => y.PropertyFromInterface, y => y.Value)
    .Switch().Subscribe(DoSomethingWith);

(例如,当我设置this.Item为 的实例时IInterface,我想DoSomethingWith听听对该实例的更改PropertyFromInterface,并且当this.Item设置为不同的东西时,可观察对象不应继续触发,直到再次this.Item成为 的实例IInterface。)

于 2014-09-01T12:52:04.190 回答