【问题标题】:ReactiveUI, WPF and ValidationReactiveUI、WPF 和验证
【发布时间】:2016-01-22 17:25:00
【问题描述】:

我已经看到 ReactiveUI 过去具有验证功能。目前,使用 6.5 版,我找不到任何相关内容。

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

【问题讨论】:

    标签: c# .net wpf validation reactiveui


    【解决方案1】:

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

    【讨论】:

    • 关于 WPF 有没有一种通用的方法来做到这一点?也许使用交互行为或将样式触发器附加到那些面向验证的属性?谢谢!
    【解决方案2】:

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

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

    视图模型

    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 的任何控件。

    希望对你有帮助。

    最好的问候。

    【讨论】:

    • OP 专门说 WPF。你给他一个 DroidExtensions 包? Droid 是“Android”的简写,不是 WPF。
    • 我只是参考视图示例。但是,任何平台都存在相同的 BindValidation 方法。
    • 谢谢亚历克斯。我不知道我在这方面做错了什么(ReactiveUI 验证),但我会弄清楚,写博客,并在此处发布 url。
    • 完美!我正在研究一个样本来阐明这个包的主要概念。您可以在示例文件夹中查看here
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多