ReactiveUI、WPF 和验证

ReactiveUI, WPF and Validation

我看到 ReactiveUI 过去有验证功能。目前,对于 6.5 版,我找不到任何与之相关的内容。

您知道在使用 ReactiveUI 的 WPF 中是否有或多或少的官方方式来处理验证任务?

RxUI 松弛组的总体共识是人们正在公开额外的验证属性,例如拆分 UserNameUserNameError(如果没有错误,则为 null)。然后利用平台的validation/error机制来引起用户的注意。

你不能看一下这个 repo https://github.com/reactiveui/ReactiveUI.Validation,也可以在 NuGet 画廊上找到。

此解决方案基于 MVVM 模式,因此您的 ViewModel 必须实施 ISupportsValidation、添加规则(ValidationHelper 属性)并绑定到视图中的验证规则。

视图模型

public class SampleViewModel : ReactiveObject, ISupportsValidation
{
    public ValidationContext ValidationContext => new ValidationContext();

    // Bindable rule
    public ValidationHelper ComplexRule { get; set; }

    public SampleViewModel()
    {
         // name must be at least 3 chars - the selector heee is the property name and its a single property validator
         this.ValidationRule(vm => vm.Name, _isDefined, "You must specify a valid name");
    }
}

查看

public class MainActivity : ReactiveAppCompatActivity<SampleViewModel>
{
    public EditText nameEdit { get; set; }

    public TextInputLayout til { get; set; }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our View from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        WireUpControls();

        // bind to an Android TextInputLayout control, utilising the Error property
        this.BindValidation(ViewModel, vm => vm.ComplexRule, til);
    }
}

View 示例正在利用 DroidExtensions(为 Mono.Droid 项目自动添加),但您可以将错误消息绑定到 View 的任何控件。

希望对您有所帮助。

此致。