订阅关闭,但仅在项目已保存时关闭

Subscribe to Close, but close only if item was saved

我的场景:

在 MVVM 模式中,在 ViewModel 上执行命令时应关闭视图,但前提是项目已成功保存。

视图看起来像:

public class CentreUpdateWindow : ReactiveWindow<CentreUpdateViewModel>
{ 
    public CentreUpdateWindow()
    {
        this.InitializeComponent();
        this.WhenActivated(d =>
           d(ViewModel!.SubmitCommand.Subscribe(CloseIfSuccessfullySaved))
        );
    }

    private void CloseIfSaved(Centre? obj)
    {
        if (ViewModel!.SuccessfullySaved)
            Close(obj);
    }

    // ...

和视图模型:

public class CentreUpdateViewModel : ViewModelBase, IId
{
    // ...

    public ReactiveCommand<Unit, dtoo.Centre?> SubmitCommand { get; }

    private bool _SuccessfullySaved;
    public bool SuccessfullySaved
    {
        get { return _SuccessfullySaved; }
        protected set { this.RaiseAndSetIfChanged(ref _SuccessfullySaved, value); }
    }

问题:

代码运行良好,但我对 if (ViewModel!.SuccessfullySaved) 感到不适应。我想应该是一种更准确地编写订阅表达式的方法。

是否有更优雅的方式 Subscribe WhenActivated 更“反应式”?

public CentreUpdateWindow()
{
    this.InitializeComponent();
    this.WhenActivated(d =>
        d(
            ViewModel
            .WhenAnyValue(x => x.SuccessfullySaved)
            .CombineLatest(ViewModel!.SubmitCommand,
                           (saved, obj) => (saved, obj))
            .Where(s => s.saved)
            .Select(s => s.obj)
            .Subscribe(Close)
        ));
}