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 メソッドでした。

さらに、プロパティが必要なタイプでない場合、オブザーバブルが何もしないようにしたかったのです。

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たかったのですが、 が別のインスタンスに設定された場合、オブザーバブルは再びのインスタンスになるまで発火し続けるべきではありません。)DoSomethingWithPropertyFromInterfacethis.Itemthis.ItemIInterface

于 2014-09-01T12:52:04.190 に答える