微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何只运行管道 ToProperty 运算符 Source is not Null

如何解决如何只运行管道 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 举报,一经查实,本站将立刻删除。