Rx.NET:this.WhenAnyValue(X => x.Foo)在构造函数会引发ArgumentNullException

问题描述 投票:1回答:1

我有一个类中,我在做类似下面的内容:

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 => ...);

我不能找到一个办法让什么错在这里更好的视野。如何获取关于此错误的更多信息?我如何解决它?

c# system.reactive reactiveui
1个回答
1
投票

为了完整起见,我会拉从评论的答案:

看来,CoreDispatcherScheduler还没有的时候创建,当你开始观察你的属性。根据我的经验,这些东西往往当你在构造函数,然后可在应用程序生命周期很早就用于使用观测的情况发生。

因此,实例移动到OnLaunched事件,而不是应用程序启动时,能有所帮助。如果可能的话,我尝试使用Init()功能,而不是建设者要连接我的观测。

© www.soinside.com 2019 - 2024. All rights reserved.