ReactiveUI Validation Does not raise Error Notification And Failed To BindValidation

Viewed 643

Hello, I try to use ReactiveUI.Validation package To Validate Wpf Simple App And Validation works i can see it in ViewModel valid state But I can not see any notification to view element I mean no red border or any type of error appear, even when using MaterialDesignPackage, Custome Templates 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>

If my EmailText does not match the rule, The button disabled, and this ok But I can not see any behavior of validation in TextBox control itself? I try it with ErrorTemplate but not work also

So how can I show an error or even get any error notifications in view

Also notice: May open it as new question

When I try to bind validation message of specific property it failed


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);
        });
    }
}

With error :

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

This error occurs in CurrentThreadScheduler.cs of ReactiveUI At Line 107

This is a code snippet error location


// 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
                }


But

When I try to bind The Total/ Ultimate ViewModel error messages it works!


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);
        });
    }
}

resources : i think may be useful
 https://github.com/reactiveui/ReactiveUI.Validation/issues/16
 https://github.com/reactiveui/ReactiveUI.Validation/pull/17
 https://github.com/reactiveui/ReactiveUI.Validation/pull/44

Packages


  <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>

Updates

I Solve the second problem With very strange modification

This must not be considered a real solution

I just listen to validation status changes

In View

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

And Now Validation Message Appear

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

Also notice: if I listen directly from viewModel the solution will also work

I tested it in another project

But here I separate it because I use different projects from UI/Backendcontains ViewModels

Also notice this will work if I listen to any other validator

So if I expose email ValidationHelper as property out of ViewModel and subscribe to it, this will work,

Also if I just subscribe/listen to email ValidationHelper from ViewModel this will work also

But remember, I still need to call MessageBox.Show("")

Notice: This will work if I Call ShowDialog() on any Window even MainWindow

So the next Solution will also work

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

1 Answers

Binding the validation to a control template is currently not supported but you can show a validation error message in the view if you bind it to a seperate textblock (Documentation).

If you just want to use the MaterialDesign HelperText to show an error message, you don't need to use ReactiveUI.Validation but could do something like this:

    // 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();

Your second problem should be fixed when you use the latest version of ReactiveUI.Validation (1.4.13).

Related