我在一个基于ReactiveUI的ViewModel中收到以下异常…
系统。NotSupportedException:不支持的类型表达式"常数"。是否错过了表达式中的成员访问前缀?
错误发生在ValidationEditorDocumentViewModel的构造函数中(SelectedValidatorClientViewModel))在另一个响应式ViewModel (ValidationEditorViewModel)中) .
我想做的是,任何时候ValidationEditorViewModel。如果要更改SelectedValidatorClientViewModel属性,则设置SelectedValidator在另一个ViewModel (RuleEditorViewModel)) .
using NextWare.ProductPortalUI.RXViewModels.RXViewModels.ValidationEditor;
using ReactiveUI;
using System;
namespace NextWare.ProductPortalUI.RXViewModels.ValidationEditor
{
public sealed partial class ValidationEditorDocumentViewModel: ReactiveObject
{
private ValidatorEditorViewModel _validationEditorViewModel;
private RuleEditorViewModel _ruleEditorViewModel;
public ValidationEditorDocumentViewModel()
{
var setValidatorRuleSet = this.WhenAnyValue(x => ValidationEditorViewModel.SelectedValidatorClientViewModel)
.Subscribe(_ => SetValidatorOnRuleSet());
}
private void SetValidatorOnRuleSet()
{
if(RuleEditorViewModel != null)
{
RuleEditorViewModel.SelectedValidator = _validationEditorViewModel.SelectedValidatorClientViewModel.Validator;
}
}
public ValidatorEditorViewModel ValidationEditorViewModel
{
get => _validationEditorViewModel;
set => this.RaiseAndSetIfChanged(ref _validationEditorViewModel, value, nameof(ValidationEditorViewModel));
}
}
}
这是被引用的ViewModel…
using NextWare.Domain.ValidationServices.Validator.ClientViewModels;
using NextWare.ProductPortalUI.SharedComponents.DomainMetaExplorer.Models;
using ReactiveUI;
using Splat;
using System;
namespace NextWare.ProductPortalUI.RXViewModels.RXViewModels.ValidationEditor
{
public sealed partial class ValidatorEditorViewModel : ReactiveObject
{
private ValidatorsClientViewModel _validatorsClientViewModel; // Contructor Injected Domain-based ViewModel
private ValidatorClientViewModel _selectedValidatorClientViewModel;
private Guid _selectedDomainElementModelReferenceId = Guid.NewGuid();
private TreeNode _selectedMetaExplorerTreeNode;
public ValidatorEditorViewModel(ValidatorsClientViewModel validatorsClientViewModel = null)
{
_validatorsClientViewModel = validatorsClientViewModel ?? Locator.Current.GetService<ValidatorsClientViewModel>();
this.WhenAnyValue(x => x.SelectedValidatorClientViewModel)
.Subscribe(_ => SetValidator());
this.WhenAnyValue(x => x.SelectedMetaExplorerTreeNode) // Happens only once at startup
.Subscribe(_ => SetValidator());
}
private void SetValidator()
{
if(SelectedValidatorClientViewModel != null && SelectedMetaExplorerTreeNode != null)
{
SelectedValidatorClientViewModel.Validator.DomainElementName = SelectedMetaExplorerTreeNode.ModelMetaData.Name;
SelectedValidatorClientViewModel.Validator.DomainElementNameSpace = SelectedMetaExplorerTreeNode.ModelMetaData.FqViewModelName;
SelectedValidatorClientViewModel.Validator.DomainElementModelReferenceId = SelectedMetaExplorerTreeNode.ModelMetaData.ModelReferenceId;
SelectedValidatorClientViewModel.Validator.AggregateRootModelReferenceId = SelectedMetaExplorerTreeNode.ParentModelMetaData.ModelReferenceId;
}
}
public ValidatorsClientViewModel ValidatorsClientViewModel
{
get => _validatorsClientViewModel;
private set => this.RaiseAndSetIfChanged(ref _validatorsClientViewModel, value, nameof(ValidatorsClientViewModel));
}
public ValidatorClientViewModel SelectedValidatorClientViewModel
{
get => _selectedValidatorClientViewModel;
set => this.RaiseAndSetIfChanged(ref _selectedValidatorClientViewModel, value, nameof(SelectedValidatorClientViewModel));
}
public TreeNode SelectedMetaExplorerTreeNode
{
get => _selectedMetaExplorerTreeNode;
set => this.RaiseAndSetIfChanged(ref _selectedMetaExplorerTreeNode, value, nameof(SelectedMetaExplorerTreeNode));
}
}
}
从文档中,WhenAnyValue从IObservableChanged<TSender,>因此,据我所知,如果ValidationEditorViewModel不为空,无论SelectedValidatorClientViewModel是否为空,都只会有一个tick through。
我很困惑为什么会抛出这个异常。
正如Anthony在评论中提到的
改变var setValidatorRuleSet = this.WhenAnyValue(x => ValidationEditorViewModel.SelectedValidatorClientViewModel)
.Subscribe(_ => SetValidatorOnRuleSet());
var setValidatorRuleSet = this.WhenAnyValue(x => x.ValidationEditorViewModel.SelectedValidatorClientViewModel)
.Subscribe(_ => SetValidatorOnRuleSet());
ValidationEditorViewModel
前加x
WhenAnyValue依赖于你有一个lambda从'x'到你的属性。