1. Concat
var source1 = Observable.Range(1, 3); var source2 = Observable.Range(1, 3); source1.Concat(source2) .Subscribe(Console.WriteLine); Console.ReadLine();
结果输出:123123
2. Merge
var source1 = Observable.Range(1, 3); var source2 = Observable.Range(1, 6); source1.Merge(source2) .Subscribe(Console.WriteLine); Console.ReadLine();
结果输出:112233456
3. Merge、CombineLatest、and Zip -the "and" and "or" in RX
IObservable<float> o1; IObservable<int> o2; IObservable<string> o3; var result=Observable.Merge(o1.Select(_ =>true),o2.Select(_ =>true),o3.Select(_ =>true));
Merge是“Or”,Merge是无状态的,一旦第一个O1满足条件,无法判定O2和O3
Subject<int> s1; Subject<int> s2; s1.CombineLatest(s2,(a,b)=>a*b).Subscribe(Console.WriteLine);
s1.OnNext(5); //Nothing happens,no values for s2
s2.OnNext(10);//10 came in,combine the 10 with whatever s1 was (5)
>>>>>50
s2.OnNext(20);//20 came in,still use the s1's latest value
>>>>>100
s1.OnNext(2);// s1 is 1,take s2's latest value(20);
>>>>>40
Subject<int> s1; Subject<int> s2; s1.Zip(s2,(a,b)=>a*b).Subscribe(Console.WriteLine);
s1.OnNext(2);//Nothing ,no pair yet
s1.OnNext(5);//still no pair
s2.OnNext(10);(2,10),send it down
s1.OnNext(10);//s2's empty,no pair
s2.OnNext(1);// 5*1
s2.OnNext(10);//10*10
s2.OnNext(100);//s1's empty,no output
4. Catch
var source1 = Observable.Range(1, 3); var source2 = Observable.Range(4, 3); source1.Catch(source2) .Subscribe(Console.WriteLine); Console.ReadLine();
结果输出:123,只要source1正确,就不输出source2。确定问题在哪
5. OnErrorResumeNext
var source1 = Observable.Throw<int>(new Exception("An error has occurred.")); var source2 = Observable.Range(4, 3); source1.OnErrorResumeNext(source2) .Subscribe(Console.WriteLine); Console.ReadLine();
不确定问题定位,确定有异常,一定要输出。456
The Select operator can translate each element of an observable sequence into another form.
var seqNum = Observable.Range(1, 5); var seqString = from n in seqNum select new string('*', (int)n); seqString.Subscribe(str => { Console.WriteLine(str); }); Console.ReadKey();
an extension of the .NET event conversion
var frm = new Form(); IObservable<EventPattern<MouseEventArgs>> move = Observable.FromEventPattern<MouseEventArgs>(frm, "MouseMove"); IObservable<System.Drawing.Point> points = from evt in move select evt.EventArgs.Location; points.Subscribe(pos => Console.WriteLine("mouse at " + pos)); Application.Run(frm);
“Where” Operation
IObservable<int> seq = Observable.Generate(0, i => i < 10, i => i + 1, i => i * i); IObservable<int> source = from n in seq where n < 15 select n; source.Subscribe(x => {Console.WriteLine(x);}); // output is 0, 1, 4, 9 Console.ReadKey();
Event Where Operation
var frm = new Form(); IObservable<EventPattern<MouseEventArgs>> move = Observable.FromEventPattern<MouseEventArgs>(frm, "MouseMove"); IObservable<System.Drawing.Point> points = from evt in move select evt.EventArgs.Location; var overfirstbisector = from pos in points where pos.X == pos.Y select pos; var movesub = overfirstbisector.Subscribe(pos => Console.WriteLine("mouse at " + pos)); Application.Run(frm);
var seq = Observable.Interval(TimeSpan.FromSeconds(1)); var bufSeq = seq.Buffer(5); //var bufSeq = seq.Buffer(TimeSpan.FromSeconds(5)); bufSeq.Subscribe(values => Console.WriteLine(values.Sum())); Console.ReadKey();