首页 > 解决方案 > 如何只运行管道 ToProperty 运算符 Source 不是 Null

问题描述

我有一个父 RX ViewModel(在 ReactiveUI 中),它有许多从属 ViewModel 作为属性,这些属性在构造函数执行后很好地分配了值。

在构造函数中,我有一系列 Observable 管道,它们为这些依赖的 RX ViewModel 的属性分配值。

下面的当前代码失败,因为每个子 ViewModel 在各自的管道执行之前都是空的。

在将值分配给它们各自的属性之前,如何对每个子 ViewModel 进行空检查?

我认为我需要将 ViewModel 作为第二个属性包含在 WhenAnyValue 操作中,然后在进入 ToProperty 操作之前检查它是否不为空,但是我不知道该怎么做。

public sealed partial class ValidationEditorDocumentViewModel: ReactiveObject 
    {

        private TreeNode _selectedTreeNode;
        private bool _showAggregateExplorer;
        private ValidatorEditorViewModel _validationEditorViewModel;
        private RuleEditorViewModel _ruleEditorViewModel;
        private FieldRuleEditorViewModel _fieldRuleEditorViewModel;
        private AggregateMetaExplorerViewModel _aggregateMetaExplorerViewModel;
        private ValidatorClientViewModel _selectedValidatorClientViewModel;

        /// <summary>
        /// Reactive Document ViewModel that exposes IObservable PipeLine of ValidatorsClientViewModel
        /// </summary>
        public ValidationEditorDocumentViewModel() 
        {

            this.WhenAnyValue(x => x.SelectedMetaExplorerTreeNode)
                .ToProperty(this, x => x.AggregateMetaExplorerViewModel.SelectedMetaExplorerTreeNode  , scheduler: RxApp.MainThreadScheduler);

            this.WhenAnyValue(x => x.ValidationEditorViewModel.SelectedValidatorClientViewModel)
                .Where(x => x != null)
                .Select(x=> x.Validator)
                .ToProperty(this, x => x.RuleEditorViewModel.SelectedValidator , scheduler: RxApp.MainThreadScheduler);

            this.WhenAnyValue(x => x.RuleEditorViewModel.SelectedRuleSet)
                .ToProperty(this, x =>  x.FieldRuleEditorViewModel.SelectedRuleSet, scheduler: RxApp.MainThreadScheduler);
              
            this.WhenAnyValue(x => x.SelectedMetaExplorerTreeNode)
                .ToProperty(this, x => x.AggregateMetaExplorerViewModel.SelectedMetaExplorerTreeNode, scheduler: RxApp.MainThreadScheduler);

        }

        public TreeNode SelectedMetaExplorerTreeNode
        {
            get => _selectedTreeNode;
            set => this.RaiseAndSetIfChanged(ref _selectedTreeNode, value, nameof(SelectedMetaExplorerTreeNode));
        }
        
        public bool ShowAggregateExplorer
        {
            get => _showAggregateExplorer;
            set => this.RaiseAndSetIfChanged(ref _showAggregateExplorer, value, nameof(ShowAggregateExplorer));
        }

        public ValidatorEditorViewModel ValidationEditorViewModel
        {
            get => _validationEditorViewModel;
            set => this.RaiseAndSetIfChanged(ref _validationEditorViewModel, value, nameof(ValidationEditorViewModel));
        }

        public RuleEditorViewModel RuleEditorViewModel
        {
            get => _ruleEditorViewModel;
            set => this.RaiseAndSetIfChanged(ref _ruleEditorViewModel, value, nameof(RuleEditorViewModel));
        }
        
        public FieldRuleEditorViewModel FieldRuleEditorViewModel
        {
            get => _fieldRuleEditorViewModel;
            set => this.RaiseAndSetIfChanged(ref _fieldRuleEditorViewModel, value, nameof(FieldRuleEditorViewModel));
        }
        
        public AggregateMetaExplorerViewModel AggregateMetaExplorerViewModel
        {
            get => _aggregateMetaExplorerViewModel;
            set => this.RaiseAndSetIfChanged(ref _aggregateMetaExplorerViewModel, value, nameof(AggregateMetaExplorerViewModel));
        }
        
        public ValidatorClientViewModel SelectedValidatorClientViewModel
        {
            get => _selectedValidatorClientViewModel;
            set => this.RaiseAndSetIfChanged(ref _selectedValidatorClientViewModel, value, nameof(AggregateMetaExplorerViewModel));
        }

    }

标签: c#system.reactivereactiveui

解决方案


推荐阅读