ReactiveUI 验证不会引发错误通知并且无法绑定验证

ReactiveUI Validation Does not raise Error Notification And Failed To BindValidation

您好, 我尝试使用 ReactiveUI.Validation package 验证 Wpf 简单应用程序 并且验证工作 i can see it in ViewModel valid state 但是我看不到任何查看元素的通知 我的意思是没有红色边框或任何类型的错误出现,即使使用 MaterialDesignPackage、Custome 模板 nothing works!

ViewModel.cs

public class AddNewUserViewModel : ReactiveValidationObject<AddNewUserViewModel>
{
    private string _email;
    public string Email
    {
        get => _email;
        set => this.RaiseAndSetIfChanged(ref _email, value);
    }

    public ReactiveCommand<Unit,Unit> Start { get; }
    public AddNewUserViewModel()
    {
        var vEmailHelper = this
            .ValidationRule(viewModel => viewModel.Email, e => e?.Length > 2, "Error Email Message");
        var canStart = this.IsValid();
        Start = ReactiveCommand.Create(() => { }, canStart);
    }
}

View.cs

public partial class AddNewUserDialogView : ReactiveWindow<AddNewUserViewModel>
{
    public AddNewUserDialogView()
    {
        ViewModel = new AddNewUserViewModel();
        InitializeComponent();
        this.WhenActivated(disposable =>
        {
            this.Bind(ViewModel, viewModel => viewModel.Email, v => v.EmailTextBox.Text)
                .DisposeWith(disposable);
            this.BindCommand(ViewModel, viewModel => viewModel.Start, v => v.ButStart).DisposeWith(disposable);
        });
    }
}

View.cs.xaml

<reactiveUi:ReactiveWindow x:TypeArguments="dialogs:AddNewUserViewModel"
                           x:Class="UsersManager.UI.Views.Body.Dialogs.AddNewUserDialogView"
                           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                           xmlns:local="clr-namespace:UsersManager.UI.Views.Body.Dialogs"
                           xmlns:reactiveUi="http://reactiveui.net"
                           xmlns:dialogs="clr-namespace:UsersManager.Backend.ViewModels.Body.Dialogs;assembly=UsersManager.Backend"
                           mc:Ignorable="d"

                           Title="test" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>

        <TextBox
            Grid.Row="0"
            x:Name="EmailTextBox"
            materialDesign:HintAssist.Hint="Name"
            materialDesign:HintAssist.HelperText="Your user name"
        >
        </TextBox>

        <Button
            Grid.Row="1"
            x:Name="ButStart"
            >Start</Button>
    </Grid>
</reactiveUi:ReactiveWindow>

如果我的 EmailText 不符合规则,按钮被禁用,这没问题 但是我在 TextBox 控件本身中看不到任何验证行为? I try it with ErrorTemplate but not work also

那么我怎样才能显示错误甚至在视图中获得任何错误通知

另请注意:May open it as new question

当我尝试绑定特定 属性 的验证消息时,它失败了


public partial class AddNewUserDialogView : ReactiveWindow<AddNewUserViewModel>
{
    public AddNewUserDialogView()
    {
        ViewModel = new AddNewUserViewModel();
        InitializeComponent();
        this.WhenActivated(disposable =>
        {
            this.Bind(ViewModel, viewModel => viewModel.Email, v => v.EmailTextBox.Text)
                .DisposeWith(disposable);
            this.BindCommand(ViewModel, viewModel => viewModel.Start, v => v.ButStart).DisposeWith(disposable);

            // here
            this.BindValidation(ViewModel, viewModel => viewModel.Email, v => v.ButStart.Content).DisposeWith(disposable);
        });
    }
}

错误:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

这个错误出现在ReactiveUI的CurrentThreadScheduler.cs 在行 107

这是代码段错误位置


// Line 85
 SchedulerQueue<TimeSpan> queue;

            // There is no timed task and no task is currently running
            if (!_running)
            {
                _running = true;

                if (dueTime > TimeSpan.Zero)
                {
                    ConcurrencyAbstractionLayer.Current.Sleep(dueTime);
                }

                // execute directly without queueing
                IDisposable d;
                try
                {
                    d = action(this, state);
                }
                catch
                {
                    SetQueue(null);
                    _running = false;
                    throw; // Line 107
                }


但是

当我尝试绑定 Total/Ultimate ViewModel 错误消息时,它起作用了!


public partial class AddNewUserDialogView : ReactiveWindow<AddNewUserViewModel>
{
    public AddNewUserDialogView()
    {
        ViewModel = new AddNewUserViewModel();
        InitializeComponent();
        this.WhenActivated(disposable =>
        {
            this.Bind(ViewModel, viewModel => viewModel.Email, v => v.EmailTextBox.Text)
                .DisposeWith(disposable);
            this.BindCommand(ViewModel, viewModel => viewModel.Start, v => v.ButStart).DisposeWith(disposable);

            // here
            this.BindValidation(ViewModel, v => v.ButStart.Content).DisposeWith(disposable);
        });
    }
}

资源:我认为可能有用
 https://github.com/reactiveui/ReactiveUI.Validation/issues/16
 https://github.com/reactiveui/ReactiveUI.Validation/pull/17
 https://github.com/reactiveui/ReactiveUI.Validation/pull/44

套餐


  <ItemGroup>
    <Reference Include="ActiproSoftware.BarCode.Wpf, Version=17.2.663.0, Culture=neutral, PublicKeyToken=36ff2196ab5654b9, processorArchitecture=MSIL" />
    <Reference Include="ActiproSoftware.Shared.Wpf, Version=17.2.663.0, Culture=neutral, PublicKeyToken=36ff2196ab5654b9, processorArchitecture=MSIL" />
    <Reference Include="DynamicData, Version=6.14.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\DynamicData.6.14.8\lib\net461\DynamicData.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="ICSharpCode.AvalonEdit, Version=5.0.3.0, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
      <HintPath>..\packages\AvalonEdit.5.0.4\lib\Net40\ICSharpCode.AvalonEdit.dll</HintPath>
    </Reference>
    <Reference Include="MaterialDesignColors, Version=1.2.2.920, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\MaterialDesignColors.1.2.2\lib\net45\MaterialDesignColors.dll</HintPath>
    </Reference>
    <Reference Include="MaterialDesignThemes.Wpf, Version=3.0.1.920, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\MaterialDesignThemes.3.0.1\lib\net45\MaterialDesignThemes.Wpf.dll</HintPath>
    </Reference>
    <Reference Include="mscorlib" />
    <Reference Include="Pharmacist.Common, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\Pharmacist.Common.1.5.15\lib\netstandard2.0\Pharmacist.Common.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="PresentationFramework.Aero" />
    <Reference Include="ReactiveUI, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\ReactiveUI.11.2.3\lib\net461\ReactiveUI.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="ReactiveUI.Events.WPF, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\ReactiveUI.Events.WPF.11.2.3\lib\net461\ReactiveUI.Events.WPF.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="ReactiveUI.Fody.Helpers, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\ReactiveUI.Fody.11.2.3\lib\net461\ReactiveUI.Fody.Helpers.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="ReactiveUI.Validation, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\ReactiveUI.Validation.1.4.10\lib\net461\ReactiveUI.Validation.dll</HintPath>
    </Reference>
    <Reference Include="ReactiveUI.WPF, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\ReactiveUI.WPF.11.2.3\lib\net461\ReactiveUI.WPF.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="ShowMeTheXAML, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\ShowMeTheXAML.1.0.12\lib\net45\ShowMeTheXAML.dll</HintPath>
    </Reference>
    <Reference Include="ShowMeTheXAML.AvalonEdit, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\ShowMeTheXAML.AvalonEdit.1.0.12\lib\net45\ShowMeTheXAML.AvalonEdit.dll</HintPath>
    </Reference>
    <Reference Include="Splat, Version=9.3.0.0, Culture=neutral, PublicKeyToken=null">
      <HintPath>..\packages\Splat.9.3.11\lib\net461\Splat.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data" />
    <Reference Include="System.Reactive, Version=4.3.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263">
      <HintPath>..\packages\System.Reactive.4.3.2\lib\net46\System.Reactive.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
      <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.3\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
      <HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System.Windows" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>

更新

我用很奇怪的修改解决了第二个问题

这不能被视为真正的解决方案

我只听验证状态变化

在视图中

ViewModel.ValidationContext.ValidationStatusChange.Subscribe(x =>
                {
                    // if i omite next line , the solution will not work
                    MessageBox.Show("Start");
                });

现在出现验证消息

 // so: now this line will not produce argument out of range exception
 this.BindValidation(ViewModel, viewModel => viewModel.Email, v => v.ButStart.Content).DisposeWith(disposable);

另请注意:如果我直接从 viewModel 收听,解决方案也将有效

I tested it in another project

但这里我把它分开了,因为我使用了与UI/Backendcontains ViewModels

不同的项目 另请注意,如果我听任何其他验证器,这将起作用

因此,如果我将电子邮件 ValidationHelper 作为 属性 从 ViewModel 公开并订阅它,这将起作用,

此外,如果我只是 subscribe/listen 从 ViewModel 向 ValidationHelper 发送电子邮件,这也可以工作

但是请记住,我仍然需要调用 MessageBox.Show("")

注意:如果我在任何 Window 甚至 MainWindow

上调用 ShowDialog(),这将起作用

所以下一个解决方案也可以工作

ViewModel.ValidationContext.ValidationStatusChange.Subscribe(x =>
                {
                    var v = new MainWindow();
                    v.ShowDialog();
                });

当前不支持将验证绑定到控件模板,但如果将其绑定到单独的文本块 (Documentation),则可以在视图中显示验证错误消息。

如果您只想使用 MaterialDesign HelperText 显示错误消息,则不需要使用 ReactiveUI.Validation,但可以这样做:

    // Viewmodel
    private ObservableAsPropertyHelper<bool> isEmailValid;
    public bool IsEmailValid => isEmailValid.Value;
    ...
    isEmailValid = this.WhenAnyValue(x => x.Email).Select(e => e?.Length > 2).ToProperty(this, nameof(IsEmailValid));

    // View
    this.WhenAnyValue(x => x.ViewModel.IsEmailValid)
        .Do(isValid => HintAssist.SetHint(EmailTextBox, isValid ? string.Empty : "Error Email Message"))
        .Subscribe();

当您使用最新版本的 ReactiveUI.Validation (1.4.13) 时,您的第二个问题应该会得到解决。