[英]ObservableForProperty for multiple Reactive Observable Sequence
I'm trying to ObservableForProperty for multiple Reactive Observable Sequences. 我正在尝试ObservableForProperty获得多个反应性可观察序列。 Here's the code.
这是代码。
using ReactiveUI;
public ReactiveUIDerivedClass<T> FName {get; private set;}
public ReactiveUIDerivedClass<T> MName {get; private set;}
public ReactiveUIDerivedClass<T> LName {get; private set;}
FName = new ReactiveUIDerivedClass<T>();
MName = new ReactiveUIDerivedClass<T>();
LName = new ReactiveUIDerivedClass<T>();
Here T is System.String . 这里T是System.String 。 So, the below method works.
因此,以下方法有效。
private void ObserveForPropertyInThreeFields()
{
Observable.CombineLatest(FName.ObservableForProperty(p => p.Value),
MName.ObservableForProperty(p => p.Value),
LName.ObservableForProperty(p => p.Value))
.Where(p => p.All(v => !string.IsNullOrWhiteSpace(v.Value)))
.Subscribe(p => { /* do some stuff */ } );
}
Now, I want to achieve similar result where T for the properties are of three different types. 现在,我想获得类似的结果,其中属性的T为三种不同类型。 When I used Observable.CombineLatest , I get the following error message.
当我使用Observable.CombineLatest时 ,收到以下错误消息。
The type arguments for method 'System.Reactive.Linq.Observable.CombineLatest (System.IObservable, System.IObservable, System.Func)' cannot be inferred from the usage. 无法从用法中推断出方法'System.Reactive.Linq.Observable.CombineLatest(System.IObservable,System.IObservable,System.Func)'的类型参数。 Try specifying the type arguments explicitly.
尝试显式指定类型参数。
Any ideas on how do I ObserveForProperty Change for three properties of different types? 关于如何为不同类型的三个属性进行ObserveForProperty Change的任何想法?
This is actually built-in to ReactiveUI already, using a better method than ObservableForProperty
, called WhenAnyValue
: 它实际上已经内置在ReactiveUI中,使用的方法比
ObservableForProperty
更好,称为WhenAnyValue
:
this.WhenAnyValue(x => x.FName, x => x.MName, x => x.LName,
(first,middle,last) => new { first, middle, last });
I'm leaving out the specific detail of using ObservableForProperty
here - it's only the fact that the types of the streams differ that is important, so we can look at this as a vanilla Rx problem. 我在这里省略了使用
ObservableForProperty
的具体细节-唯一重要的事实是流的类型不同,因此我们可以将其视为普通的Rx问题。
If you have streams with differing types then obviously the compiler cannot merge them into a single type itself. 如果您具有不同类型的流,则显然编译器无法将它们本身合并为单个类型。 You need to specify a result selector function as the final argument to
CombineLatest
to tell it how you want to do this: eg given the following source streams: 您需要指定一个结果选择器函数作为
CombineLatest
的最后一个参数,以告诉它您如何执行此操作:例如,给定以下源流:
IObservable<T1> xs;
IObservable<T2> ys;
IObservable<T3> zs;
...then one easy option is to produce an anonymous type (as long as you aren't returning it outside of the function of course) eg: ...那么一个简单的选择是产生一个匿名类型(只要您当然不在函数之外返回它),例如:
Observable.CombineLatest(xs, ys, zs, (x,y,z) => new { x,y,z });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.