Rx.NET: this.WhenAnyValue(x => x.Foo) 在构造函数中抛出 ArgumentNullException

Rx.NET: this.WhenAnyValue(x => x.Foo) in constructor throws ArgumentNullException

我有一个 class,其中我正在做类似于以下的事情:

public class Foo : ReactiveObject
{
    // The constructor that sets up a subscription
    public Foo()
    {
        this.WhenAnyValue(foo => foo.Bar)
            .Where(bar => bar != null)
            .Subscribe(bar => ...);
    }

    // Reactive property
    private IBar _bar;
    public IBar Bar
    {
        get { return _bar; }
        set { this.RaiseAndSetIfChanged(ref _bar, value); }
    }
}

现在,在构建实例时,出现以下错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: dispatcher
   at System.Reactive.Concurrency.CoreDispatcherScheduler..ctor(CoreDispatcher dispatcher)}

为了确保我没有对我的实例做一些愚蠢的事情,我将订阅分成几部分,仅用于测试:

var observable = this.WhenAnyValue(foo => foo.Bar); // <-- throws already on this line!
var nonulls = observable.Where(bar => bar != null);
var subscription = nonulls.Subscribe(bar => ...);

我找不到更好地了解这里出了什么问题的方法。我如何获得有关此错误的更多信息?我该如何解决?

为了完整起见,我将从评论中提取答案:

在您开始观察您的属性时,CoreDispatcherScheduler 似乎尚未创建。根据我的经验,当您在构造函数中使用可观察对象时,这些事情往往会发生,然后可能会在应用程序生命周期的早期使用。

因此,将实例化移至 OnLaunched 事件而不是应用程序启动可能会有所帮助。如果可能,我会尝试使用 Init() 函数而不是构造函数来连接我的可观察对象。