在.Net中什么是Rx框架?

在.Net中什么是Rx框架?,.net,observablecollection,.net,Observablecollection,Net中的Rx框架是什么?我已经阅读了一些关于它的文章,但我仍然不清楚它的概念。任何人都可以用一个实际的例子来解释它。Rx或Reactive Extensions是一个库,它使用可观察的集合和LINQ风格的查询操作符来编写异步和基于事件的程序 网站上有很多文章,例如: 还有我们自己的 见及 Rx或反应式扩展是一个库,用于使用可观察集合和LINQ样式的查询运算符组合异步和基于事件的程序 网站上有很多文章,例如: 还有我们自己的 见及 Rx或无功扩展是“LINQ到事件”。LINQ

Net中的Rx框架是什么?我已经阅读了一些关于它的文章,但我仍然不清楚它的概念。任何人都可以用一个实际的例子来解释它。

Rx或Reactive Extensions是一个库,它使用可观察的集合和LINQ风格的查询操作符来编写异步和基于事件的程序

网站上有很多文章,例如:

还有我们自己的

见及


Rx或反应式扩展是一个库,用于使用可观察集合和LINQ样式的查询运算符组合异步和基于事件的程序

网站上有很多文章,例如:

还有我们自己的

见及


Rx或无功扩展是“LINQ到事件”。LINQ基于
IEnumerable
,通过迭代从序列中提取项:

var items = source.Where(x => x.Name == "foobar");
foreach (var item in items)
  Console.WriteLine(item);
Rx是基于
IObservable
的反向,您订阅推送到您的一系列事件:

var events = source.Where(x => x.Name == "foobar");
events.Subscribe(item => Console.WriteLine(item));
在LINQ中,您可以通过使用
foreach
循环和从序列中拉出项来控制迭代。在Rx中,其他东西触发事件,并在项目准备就绪时将其推送给您


Rx向
IObservable
添加了大量扩展方法,正如LINQ向
IEnumerable
添加了大量扩展方法,使您能够编写非常紧凑、清晰的代码来处理异步事件。

Rx或反应式扩展是“LINQ到事件”。LINQ基于
IEnumerable
,通过迭代从序列中提取项:

var items = source.Where(x => x.Name == "foobar");
foreach (var item in items)
  Console.WriteLine(item);
Rx是基于
IObservable
的反向,您订阅推送到您的一系列事件:

var events = source.Where(x => x.Name == "foobar");
events.Subscribe(item => Console.WriteLine(item));
在LINQ中,您可以通过使用
foreach
循环和从序列中拉出项来控制迭代。在Rx中,其他东西触发事件,并在项目准备就绪时将其推送给您


Rx向
IObservable
添加了许多扩展方法,正如LINQ向
IEnumerable
添加了许多扩展方法,使您能够编写非常紧凑、清晰的代码来处理异步事件。

让我们想象一下以下场景:在漫长的一天之后,您和您的朋友进入一家酒吧解渴。您只需要几杯啤酒和一张桌子,在那里您可以讨论各种主题(地图减少实施、气体价格等)

在池方法(非Rx)中,您将执行以下步骤:

1. Ask bartender for two beers
2. While bartender pours the beer into the glasses you wait by the bar
3. When bartender gives you the beers you take them and find an available table, sit, drink and discuss
1. Ask the bartender for two beers
2. Bartender starts pouring your beers and you and your frined find an available table, sit and  start discussing on various subjects
3. When bartender finishes pouring your beer, a waitress brings the beer to your table and you start drinking it.
使用Rx方法,您将执行以下步骤:

1. Ask bartender for two beers
2. While bartender pours the beer into the glasses you wait by the bar
3. When bartender gives you the beers you take them and find an available table, sit, drink and discuss
1. Ask the bartender for two beers
2. Bartender starts pouring your beers and you and your frined find an available table, sit and  start discussing on various subjects
3. When bartender finishes pouring your beer, a waitress brings the beer to your table and you start drinking it.
现在可以将上述内容转换为以下代码:

//Non Rx approach
var beers = beerKeg.Take(2); // Here you wait for the bartender to pour the beer
foreach(var beer in beers)   // Bartender handles you the beer
    GrabBeer(beer);
FindAvailableTable();        // You search for an available table
foreach(var beer in beers)   // You drink your beer
    beer.Drink();

//Rx approach
var beers = beerKeg.Take(2) 
                   .ToObservable() 
                   .Subscribe(beer => beer.Drink());
// Bartender is pouring the beer but meanwhile you can search for an available table.
// The waitress will bring beer to you.
FindAvailableTable();
希望这有帮助


PS:To参见差异使用;执行非接收查询并调用结果上的
Dump()
-结果将显示在网格中。使用Rx执行相同的查询,并使用
x=>x.Dump()订阅
ObservableCollection
;您将看到每个元素都是单独打印的。

让我们想象以下场景:漫长的一天之后,您和您的朋友进入一家酒吧解渴。您只需要几杯啤酒和一张桌子,在那里您可以讨论各种主题(地图减少实施、气体价格等)

在池方法(非Rx)中,您将执行以下步骤:

1. Ask bartender for two beers
2. While bartender pours the beer into the glasses you wait by the bar
3. When bartender gives you the beers you take them and find an available table, sit, drink and discuss
1. Ask the bartender for two beers
2. Bartender starts pouring your beers and you and your frined find an available table, sit and  start discussing on various subjects
3. When bartender finishes pouring your beer, a waitress brings the beer to your table and you start drinking it.
使用Rx方法,您将执行以下步骤:

1. Ask bartender for two beers
2. While bartender pours the beer into the glasses you wait by the bar
3. When bartender gives you the beers you take them and find an available table, sit, drink and discuss
1. Ask the bartender for two beers
2. Bartender starts pouring your beers and you and your frined find an available table, sit and  start discussing on various subjects
3. When bartender finishes pouring your beer, a waitress brings the beer to your table and you start drinking it.
现在可以将上述内容转换为以下代码:

//Non Rx approach
var beers = beerKeg.Take(2); // Here you wait for the bartender to pour the beer
foreach(var beer in beers)   // Bartender handles you the beer
    GrabBeer(beer);
FindAvailableTable();        // You search for an available table
foreach(var beer in beers)   // You drink your beer
    beer.Drink();

//Rx approach
var beers = beerKeg.Take(2) 
                   .ToObservable() 
                   .Subscribe(beer => beer.Drink());
// Bartender is pouring the beer but meanwhile you can search for an available table.
// The waitress will bring beer to you.
FindAvailableTable();
希望这有帮助


PS:To参见差异使用;执行非接收查询并调用结果上的
Dump()
-结果将显示在网格中。使用Rx执行相同的查询,并使用
x=>x.Dump()订阅
ObservableCollection
;您将看到每个元素都是单独打印的。

这是一种简化的观察者模式,您可以订阅事件或观察者收集中的任何更改,这对MVVM模式非常有用。Phil更好地解释了这一点,并给出了一个实际的例子:Observator模式使您可以轻松订阅事件或observablecollection中的任何更改,这对MVVM模式非常有用。Phil解释得更好,并举了一个实际例子感谢Martin+1对它的解释Hanks Martin+1对它的解释