ReactiveUI, WPF and Validation

本文关键字:Validation and WPF ReactiveUI | 更新日期: 2023-09-27 18:31:39

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

您是否知道是否有一种或多或少的官方方法可以使用 ReactiveUI 处理 WPF 中的验证任务?

ReactiveUI, WPF and Validation

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

无法查看此存储库 https://github.com/reactiveui/ReactiveUI.Validation,也可在 NuGet 库中找到。

此解决方案基于 MVVM 模式,因此您的 ViewModels 必须实现 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 的任何控件。

我希望它有所帮助。

此致敬意。