C# 反应性和浇铸性
我现在做的是:C# 反应性和浇铸性,c#,reactiveui,C#,Reactiveui,我现在做的是: Item.PropertyChanged += (sender, args) => { if(sender is IInterface) DoSomethingWith(((IInterface)sender).PropertyFromInterface); } 我将如何在RxUI中实现这样一个流 我试过这个: this.WhenAny(x => (x.Item as IInterface).PropertyFromInterface, x.
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);
this.WhenAny(x=>x.Item,x=>x.GetValue())of type()的
.Select(x=>x.PropertyFromInterface).DistinctUntilChanged()
.认购(DoSomethingWith);
但我真正想要的是在项目为界面时获得“PropertyFromInterface”的propertychanged更新。怎么样:
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));
那么:
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));
回头看看我以前的问题,我在寻找这样的解决方案:
this.WhenAny(x => x.Item, x => x.GetValue()).OfType<IInterface>()
.Select(x => x.WhenAny(y => y.PropertyFromInterface, y => y.Value).Switch()
.Subscribe(DoSomethingWith);
(例如,当我将
this.Item
设置为IInterface
的实例时,我希望DoSomethingWith
侦听该实例的属性从接口的更改,并且当this.Item
设置为不同的值时,在this.Item
成为实例之前,不应继续激发可观测值。)再次使用界面)检查我以前的问题,我正在寻找类似的解决方案:
this.WhenAny(x => x.Item, x => x.GetValue()).OfType<IInterface>()
.Select(x => x.WhenAny(y => y.PropertyFromInterface, y => y.Value).Switch()
.Subscribe(DoSomethingWith);
(例如,当我将this.Item
设置为IInterface
的实例时,我希望DoSomethingWith
侦听该实例的属性从接口的更改,并且当this.Item
设置为不同的值时,在this.Item
成为实例之前,不应继续激发可观测值。)再次使用i界面的
)我想绑定到接口上定义的属性,而不是对象本身。每当替换项本身时,而不是当它的属性更改时,这将抛出。向OP中添加一些内容以澄清问题。你实际上想做什么?我不知道你为什么想要这样的构造。我有一个带有viewmodelbase的导体viewmodel项目。如果项目当前属于某个接口,则应观察它当时拥有的某个属性。在此之前,我已注册/取消注册到项目上的propertychanged(每当它被替换时)并询问它是否属于接口。我希望我可以放弃注册/取消注册并编写一个observable,它可以自己完成这项工作。我希望绑定到接口上定义的属性,而不是对象本身。这将在项目本身被替换时抛出,而不是在其属性被更改时抛出。向OP添加一些内容到clarify.你到底想做什么?我不知道你为什么想要这样的构造我有一个导体viewmodel,其中viewmodelbase是Item。如果该项当前是某个接口,那么应该观察它当时拥有的某个属性。在此之前,我注册/取消注册到该项上的propertychanged(每当它被替换时)我希望我可以放弃注册/取消注册,写一个可以自己完成任务的可观察对象。