C# 访问.NET标准库中的Dispatcher IsScheduler
描述如何通过将UI调度程序传递到C# 访问.NET标准库中的Dispatcher IsScheduler,c#,.net,system.reactive,.net-standard,C#,.net,System.reactive,.net Standard,描述如何通过将UI调度程序传递到Throttle,确保IObservable.Throttle在UI线程上运行。然而,我的观测值是在基于.NET标准1.5的共享库中定义的。在.NET标准中是否存在与DispatchersScheduler.Instance等效的程序?,还是应该从使用的(WPF/UWP/etc)库中插入计划程序?类DispatchersScheduler不存在于.NET标准的System.Responsive程序集中 一种可能的解决方法是将SynchronizationConte
Throttle
,确保IObservable.Throttle
在UI线程上运行。然而,我的观测值是在基于.NET标准1.5的共享库中定义的。在.NET标准中是否存在与DispatchersScheduler.Instance等效的程序?,还是应该从使用的(WPF/UWP/etc)库中插入计划程序?类DispatchersScheduler不存在于.NET标准的System.Responsive程序集中
一种可能的解决方法是将SynchronizationContextScheduler
与当前的SynchronizationContext
一起使用。当调用ObserveOn()
时,当您在UI线程上时,可观察对象的处理程序将在UI线程上运行
如果未在UI线程上调用ObserveOn()
ist,则整个过程都无法工作。为了避免这种情况,您可以创建类似于AppContext
对象的对象,在UI初始化时进行设置,并使其可在应用程序中分发。在此AppContext
中保存SynchronizationContext.Current
,您可以从拥有AppContext
的任何地方在UI线程上创建观察者
IObservable<EventArgs> myObservable = GetSomeObservableEvents();
var myScheduler = new SynchronizationContextScheduler(SynchronizationContext.Current);
myObservable.ObserveOn(myScheduler).Subscribe(ev =>
{
// some code that should run on UI thread
});
IObservable myObservable=getsomeobserveeevents();
var myScheduler=new SynchronizationContextScheduler(SynchronizationContext.Current);
myObservable.ObserveOn(myScheduler).Subscribe(ev=>
{
//一些应该在UI线程上运行的代码
});
我在Xamarin.Forms 2.5.0项目中这样使用它,在.NET Standard 2.0中有一个包含ViewModels、Views等的应用程序项目
在以下情况下,此答案是有效的(至少):
- .NET标准项目版本为2.0
- System.Reactive的版本为4.0.0-preview00001