How can I send a PropertyChanged event from an interval-based IObservable subscription

I get an “UnauthorizedAccesExpection - Invalid Pass Through" exception when I try to raise a PropertyChanged event from a subscription to an IObservable collection created through Observable.Interval ().

With my limited thread knowledge, I assume that the interval occurs on some other thread, while the event wants to happen on the user interface thread ??? An explanation of the problem would be very helpful.

The code looks a bit:

var subscriber = Observable.Interval(TimeSpan.FromSeconds(1))
                .Subscribe(x =>
                {
                    Prop = x; // setting property raises a PropertyChanged event
                });

Any solutions?

Edit:

This code is executed from the ViewModel, not the DependencyObject.

+3
source share
2

: SubscribeOn ObserveOn.

, :

var subscriber = Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.Dispatcher) 
                .Subscribe(x => 
                { 
                    Prop = x; // setting property raises a PropertyChanged event 
                }); 

ObserveOnDispatcher, , :

var subscriber = Observable.Interval(TimeSpan.FromSeconds(1)) 
                .ObserveOnDispatcher()
                .Subscribe(x => 
                { 
                    Prop = x; // setting property raises a PropertyChanged event 
                }); 
+2

: -

var subscriber = Observable.Interval(TimeSpan.FromSeconds(1))
            .Subscribe(x =>
            {
                Dispatcher.BeginInvoke(() => Prop = x);
            });

Edit

ViewModel, . : Silverlight.

+1

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


All Articles