【发布时间】:2016-01-22 17:25:00
【问题描述】:
我已经看到 ReactiveUI 过去具有验证功能。目前,使用 6.5 版,我找不到任何相关内容。
你知道在 WPF 中使用 ReactiveUI 处理验证任务是否有或多或少的官方方式?
【问题讨论】:
标签: c# .net wpf validation reactiveui
我已经看到 ReactiveUI 过去具有验证功能。目前,使用 6.5 版,我找不到任何相关内容。
你知道在 WPF 中使用 ReactiveUI 处理验证任务是否有或多或少的官方方式?
【问题讨论】:
标签: c# .net wpf validation reactiveui
关于 RxUI 松弛组的总体共识是人们公开了额外的验证属性,例如拆分UserName 和UserNameError(如果没有错误,则为null)。然后使用平台的验证/错误机制来引起用户的注意。
【讨论】:
你不能看这个 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 的任何控件。
希望对你有帮助。
最好的问候。
【讨论】: