如何为类创建可观察量? 例如,这里是持续销售的类
public class Product
{
public int ProductId {get;set;}
public string ProductDescription {get;set;}
public float Sales{get;set;}
}
如何创建可观察?假设他们正在与 Add((List<Product>
,产品不断被添加代表事务。
任何时候,将产品添加到控制台程序中的任何位置,订阅者都会观察它。
var productSample1 = new Product { ProductId = 1, ProductDescription = "Furniture" };
var productSample2 = new Product { ProductId = 2, ProductDescription = "Book" };
var productSample3 = new Product { ProductId = 3, ProductDescription = "car" };
var productlist = new List<Product>();
productlist.Add(productSample1);
....
productlist.Add(productSample2);
...
productlist.Add(productSample3);
也许是这样的,
var products = Observable.FromEventPattern < EventHandler<Product>, Product>();
尝试阅读下面的资源,尝试添加 ,
使用 rx 以随机时间间隔生成数字
Rx 简介
您可以使用 ObservableCollection:
var list = new ObservableCollection<int>();
using (var o = Observable
.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(h => list.CollectionChanged += h, h => list.CollectionChanged -= h)
.Subscribe(e => Console.WriteLine($"{e.EventArgs.Action} {e.EventArgs.NewItems[0]}")))
{
list.Add(1);
list.Add(10);
}
// Add 1
// Add 10
或者将其转过来并从订阅中添加:
var list = new List<int>();
var subject = new Subject<int>();
using (var o = subject.Subscribe(i => list.Add(i)))
{
subject.OnNext(1);
subject.OnNext(10);
}
foreach (var i in list)
{
Console.WriteLine(i);
}
// 1
// 10