如何只运行管道 ToProperty 运算符 Source is not 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,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,nameof(ShowAggregateExplorer));
        }

        public ValidatorEditorviewmodel ValidationEditorviewmodel
        {
            get => _validationEditorviewmodel;
            set => this.RaiseAndSetIfChanged(ref _validationEditorviewmodel,nameof(ValidationEditorviewmodel));
        }

        public RuleEditorviewmodel RuleEditorviewmodel
        {
            get => _ruleEditorviewmodel;
            set => this.RaiseAndSetIfChanged(ref _ruleEditorviewmodel,nameof(RuleEditorviewmodel));
        }
        
        public FieldRuleEditorviewmodel FieldRuleEditorviewmodel
        {
            get => _fieldRuleEditorviewmodel;
            set => this.RaiseAndSetIfChanged(ref _fieldRuleEditorviewmodel,nameof(FieldRuleEditorviewmodel));
        }
        
        public AggregateMetaExplorerviewmodel AggregateMetaExplorerviewmodel
        {
            get => _aggregateMetaExplorerviewmodel;
            set => this.RaiseAndSetIfChanged(ref _aggregateMetaExplorerviewmodel,nameof(AggregateMetaExplorerviewmodel));
        }
        
        public ValidatorClientviewmodel SelectedValidatorClientviewmodel
        {
            get => _selectedValidatorClientviewmodel;
            set => this.RaiseAndSetIfChanged(ref _selectedValidatorClientviewmodel,nameof(AggregateMetaExplorerviewmodel));
        }

    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...