c# - ReactiveUI 验证不会引发错误通知并且无法 BindValidation

标签 c# wpf reactiveui

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


查看.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);
        });
    }
}

查看.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
那么我怎样才能显示错误甚至在 View 中获得任何错误通知

另请注意:May open it as new question
当我尝试绑定(bind)特定属性的验证消息时,它失败了

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.

此错误发生在 CurrentThreadScheduler.cs响应式(Reactive)UI
在线 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
                }





当我尝试绑定(bind) 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>


更新

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

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

我只听验证状态的变化

在 View 中
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/Backend 的不同项目 contains ViewModels
另请注意,如果我听任何其他验证器,这将起作用

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

此外,如果我只是订阅/收听来自 ViewModel 的电子邮件 ValidationHelper,这也可以

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

注意:如果我在任何窗口甚至 MainWindow 上调用 ShowDialog(),这将起作用

所以下一个解决方案也将起作用
ViewModel.ValidationContext.ValidationStatusChange.Subscribe(x =>
                {
                    var v = new MainWindow();
                    v.ShowDialog();
                });

最佳答案

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

如果您只想使用 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) 时,您的第二个问题应该得到解决。 .

关于c# - ReactiveUI 验证不会引发错误通知并且无法 BindValidation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60887258/

相关文章:

reactiveui - ReactiveCommand 在 ReactiveUI 6 中停止工作

c# - 我应该使用 dispatcher.Invoke(...) 还是 ObserveOn(dispatcher)?

c# - 通用列表上的 is-operator

c# - Kinect 框架异步到达

wpf - 使用TemplateBinding更新源

c# - Winforms WPF Interop - WPF 内容无法绘制

xaml - "Hello World",ReactiveUI、Xamarin Forms 和 XAML 锁定

c# - 用作变量的类型

c# - 在 .Net Hierarchy 中隐藏方法

c# - 2D 绘图性能(GDI+ 与 SlimDX)