如何为课程创建一个Observable?例如,这里是连续销售的课程
public class Product
{
public int ProductId {get;set;}
public string ProductDescription {get;set;}
public float Sales{get;set;}
}
如何创建可观察的?假设他们通过Add()进入List<Product>
,产品将不断增加以代表交易。
也许像这样,
var products = Observable.FromEventPattern < EventHandler<Product>, Product>();
[尝试阅读下面的资源,尝试添加,
您可以使用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