When working with the Rx you need to keep in mind the duality between IEnumerable<T>and IObservable<T>(and IEnumerator<T>and IObserver<T>).
You should always look for objects that implement IEnumerable<T>, and consider how you would replace them with IObservable<T>.
, , List<int>, , . IObservable<int>. ( ObservableCollection<int>), Rx .
, .
, :
var dispatchTimer = new DispatcherTimer();
var random = new Random();
var listBox = new ListBox();
dispatchTimer:
IObservable<IEvent<EventArgs>> ticks =
Observable.FromEvent(
h => dispatchTimer.Tick += h,
h => dispatchTimer.Tick -= h);
:
IObservable<int> randomNumbers =
from tick in ticks
select random.Next(1, 11);
, :
_updateListBoxSubscription =
randomNumbers.ObserveOnDispatcher().Subscribe(n => listBox.Items.Add(n));
.ObserveOnDispatcher() , .
, , . , , , Rx .
private IDisposable _updateListBoxSubscription;
- , , , .
. , .