ObservableAsPropertyHelper 不触发任何通知

ObservableAsPropertyHelper doesn't fire any notification

我有这个简单的 ViewModel。

public class FrameProcessingViewModel : ReactiveObject
{
    private readonly ObservableAsPropertyHelper<LightWeight> currentDetectionExposer;

    public FrameProcessingViewModel(UnitFactory factory)
    {
        var identifications = factory.Units.SelectMany(unit => unit.Identifications);

        identifications.ToProperty(this, model => model.CurrentDetection, out currentDetectionExposer);
        identifications.Subscribe();
    }

    public LightWeight CurrentDetection => currentDetectionExposer.Value;
}

我在 CurrentDetection 属性 的视图中有一个绑定,但它没有更新。它总是空的,我不明白为什么。

我做错了什么?

编辑:

好的,我发现问题出在哪里了。唯一到达的 "unit" 项目是在调用 ToProperty 之前完成的,因此 currentDetectionExposer 的基础订阅是在项目到达之后进行的,并且从未发生任何更新。

我的 observable 依赖于 2 个 ISubject 来源。我解决了这个问题,使它们都成为 ReplaySubjects,因此每次订阅时都会推送它们的值,但它不起作用!

以下对我来说很好用 - 你确定你的 identificactions observable 曾经产生过一个值吗?

一些额外的注意事项:identifications.Subscribe() 不是必需的 - ToProperty 在内部进行订阅,这会导致您的可观察对象在寒冷时开始产生值。此外,您通常希望在 ToProperty(..) 之前放置一个 ObserveOn(RxApp.MainThreadScheduler) 以确保在后台生成的值不会意外导致 UI 从非-调度线程

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel(Observable.Interval(TimeSpan.FromSeconds(1)));
    }
}


public class ViewModel : ReactiveObject
{
    private readonly ObservableAsPropertyHelper<long> _propertyHelper;

    public ViewModel(IObservable<long> factory)
    {
        factory.ObserveOn(RxApp.MainThreadScheduler).ToProperty(this, model => model.Property, out _propertyHelper);
    }

    public long Property => _propertyHelper.Value;
}

好的,我发现问题出在哪里了。唯一到达的 "unit" 项目是在调用 ToProperty 之前完成的,因此 currentDetectionExposer 的基础订阅是在项目到达之后进行的,并且从未发生任何更新。

我的 observable 依赖于 2 个 ISubject 来源。我解决了它,使它们都成为 ReplaySubjects,因此每次订阅时都会推送它们的值,但它不起作用!