Help with a simple RX sample for this

I was looking for a sample for this, but could not find what explains how to configure it using RX: I have this requirement ...

  • In a WPF application, I have a list box
  • The manager timer procedure adds some random numbers to the local list every 2 seconds.
  • Now I want to configure the observable / observer to view this list <int> as it continues to grow and add the most recently added number to the collection of list items.

It sounds very simple, and I made the third bit in the background stream (without RX, but with a standard search in the list <int>) and easily added to the list. When trying to do the same without a background worker, etc. And just using RX, I'm stuck.

Sorry for the possible stupid question (RX experts are there for you), but please help how to get this WPF using RX.

Thank.

+3
source share
3 answers

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;

- , , , .

. , .

+9

, , , . unit test , Dispatcher Frame (yuck!).

, DispatcherTimer, Rx Scheduling Interval.

,

var random = new Random();
var ticks = Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.Dispatcher);
var randomNumbers = from tick in ticks
                    select random.Next(1, 11);
_updateListBoxSubscription = randomNumbers
    .Subscribe(n => listBox.Items.Add(n));

, DispatcherScheduler, , , .

, (IScheduler), .

var random = new Random();
var ticks = Observable.Interval(TimeSpan.FromSeconds(1), _schedulerProvider.Dispatcher);
var randomNumbers = from tick in ticks
                    select random.Next(1, 11);
_updateListBoxSubscription = randomNumbers
    .Subscribe(n => listBox.Items.Add(n));

TestScheduler DispatcherScheduler.

scheduling, testing Rx introduction.
. Rx IntroToRx.com,

+4

I don’t quite understand what you are doing, but it seems that the problem List<int>here is not the “native” Rx support, since it does not have any means to notify observers when adding a new element.

When you subscribe to it List<int>, it immediately just uploads the current content to the subscriber - it will not hang out and listen to additional changes.

May I suggest that ObservableCollection<T>is probably more suitable for you?

+1
source

Source: https://habr.com/ru/post/1769491/


All Articles