Why don't Bind and OneWayBind always use the main UI thread?

情到浓时终转凉″ 提交于 2019-12-10 15:24:01

问题


In my ViewModel I'll often have code like the following:

public bool IsDownloading { get { return _isDownloading.Value; } }
private ObservableAsPropertyHelper<bool> _isDownloading;

...

FileDownloader.WhenAny(x => x.IsDownloading, x => x.Value)
   .ToProperty(this, x => x.IsDownloading, out _isDownloading, false, RxApp.MainThreadScheduler);

Note the last RxApp.MainThreadScheduler argument - that makes sure that all updates to the IsDownloading property happen on the main UI thread.

The reason for that is because of what might subscribe to it. For example, in my view xaml I might have the following code:

this.OneWayBind(ViewModel, x => x.IsDownloading, y => y.DownloadProgress.IsActive);

Here you see that IsDownloading is going to be routed to the IsActive item on a control - so any updates must occur on the UI thread. So all of this works.

But I don't get why ReactiveUI was designed this way. It seems like it is important to the View, not the VM, that the updates occur on the UI thread, so shouldn't Bind or OneWayBind make sure it is on the right thread? In fact, if you look at the XML documentation for those routines, they talk about attaching things to View's, so the implication is that it should always be on the main thread.

So: Why doesn't OneWayBind and Bind implicitly force the MainThread? Have I missed something in how ReactiveUI works (or can be made to work)?

来源:https://stackoverflow.com/questions/32409024/why-dont-bind-and-onewaybind-always-use-the-main-ui-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!