更新视图 - 响应式扩展
Update view - Reactive Extensions
我有以下代码:
Observable.Interval(TimeSpan.FromMilliseconds(2500)).SubscribeOn(XXX).ObserveOn(YYY).Subscribe(
t => SendCounter(t),
e => HandleException(e));
其中 XXX、YYY 为 Schedulers
。
在 SendCounter(t) 中,我设置了一个带有 t 值的文本。
问题是,当我 运行 代码时,我得到了这个错误:
'only the original thread that created a view hierarchy can touch its views'
确保您在主线程上观察,以便您可以直接影响视图。
var uiThread = SynchronizationContext.Current;
Observable
.Interval(TimeSpan.FromMilliseconds(2500))
.SubscribeOn(XXX)
.ObserveOn(uiThread)
.Subscribe( t => SendCounter(t), e => HandleException(e));
如果您使用的是 ReactiveUI,请像这样使用您的代码,例如:
searchButton
.SelectMany(async x => await executeSearch(x))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.SearchResults, out searchResults);
我有以下代码:
Observable.Interval(TimeSpan.FromMilliseconds(2500)).SubscribeOn(XXX).ObserveOn(YYY).Subscribe( t => SendCounter(t), e => HandleException(e));
其中 XXX、YYY 为 Schedulers
。
在 SendCounter(t) 中,我设置了一个带有 t 值的文本。
问题是,当我 运行 代码时,我得到了这个错误:
'only the original thread that created a view hierarchy can touch its views'
确保您在主线程上观察,以便您可以直接影响视图。
var uiThread = SynchronizationContext.Current;
Observable
.Interval(TimeSpan.FromMilliseconds(2500))
.SubscribeOn(XXX)
.ObserveOn(uiThread)
.Subscribe( t => SendCounter(t), e => HandleException(e));
如果您使用的是 ReactiveUI,请像这样使用您的代码,例如:
searchButton
.SelectMany(async x => await executeSearch(x))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.SearchResults, out searchResults);