Xamarin.forms 如何在ReactiveCommand中实现CanExecute
在我们的Xamarin.Forms应用程序中,我们使用的是ReactiveProperty框架() 请注意,这里我们只使用ReactiveProperty,不使用ReactiveUI 我有一个SignInViewMode类,如下所示Xamarin.forms 如何在ReactiveCommand中实现CanExecute,xamarin.forms,reactive-programming,reactive-property,Xamarin.forms,Reactive Programming,Reactive Property,在我们的Xamarin.Forms应用程序中,我们使用的是ReactiveProperty框架() 请注意,这里我们只使用ReactiveProperty,不使用ReactiveUI 我有一个SignInViewMode类,如下所示 public class SignInPageViewModel { // Constructor public SignInPageViewModel() { } // Reactive Properties p
public class SignInPageViewModel
{
// Constructor
public SignInPageViewModel()
{
}
// Reactive Properties
public ReactiveProperty<string> PhoneNumber { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> UserName { get; set; } = new ReactiveProperty<string>();
//Commands
public ReactiveCommand GoToNextCommand { get; set; } = new ReactiveCommand();
}
public类signingPageViewModel
{
//建造师
public signingPageViewModel()
{
}
//反应性
public ReactiveProperty PhoneNumber{get;set;}=new ReactiveProperty();
public ReactiveProperty用户名{get;set;}=new ReactiveProperty();
//命令
public ReactiveCommand GoToNextCommand{get;set;}=new ReactiveCommand();
}
GoToNextCommand绑定到视图中的按钮。我想实现GoToNextCommand的CanExecute功能(如果UserName或PhoneNumber属性中的任何属性为null,则禁用GoToNextCommand),但我不知道如何在ReactiveProperty中实现这一点
感谢您的帮助 谢谢你使用我的图书馆 您可以从IObservable创建ReactiveCommand实例,如下所示
GoToNextCommand = Observable.CombineLatest(
PhoneNumber.Select(x => !string.IsNullOrEmpty(x)),
UserName.Select(x => !string.IsNullOrEmpty(x)),
(x, y) => x && y)
.ToReactiveCommand();
并且可以一起使用验证功能
// using System.ComponentModel.DataAnnotations;
[Required]
public ReactiveProperty<string> PhoneNumber { get; }
[Required]
public ReactiveProeprty<string> UserName { get; }
public ReactiveCommand GoToNextCommand { get; }
public SignInPageViewModel()
{
PhoneNumber = new ReactiveProperty<string>()
.SetValidateAttribute(() => PhoneNumber);
UserName = new ReactiveProperty<string>()
.SetValidateAttribute(() => UserName);
GoToNextCommand = Observable.CombineLatest(
PhoneNumber.ObserveHasErrors.Inverse(),
UserName.ObserveHasErrors.Inverse(),
(x, y) => x && y)
.ToReactiveCommand()
.WithSubscribe(() => { ... do something ... });
}
//使用System.ComponentModel.DataAnnotations;
[必需]
公共ReactiveProperty电话号码{get;}
[必需]
public ReactiveProeprty用户名{get;}
public ReactiveCommand GoToNextCommand{get;}
public signingPageViewModel()
{
PhoneNumber=新的反应属性()
.SetValidateAttribute(()=>电话号码);
用户名=新的反应属性()
.SetValidateAttribute(()=>用户名);
GoToNextCommand=Observable.CombineTest(
PhoneNumber.ObserveHasErrors.Inverse(),
UserName.ObserveHasErrors.Inverse(),
(x,y)=>x&&y)
.ToReactiveCommand()
.with subscribe(()=>{…做点什么…});
}
谢谢Kazuki。我还有一个关于被动属性的问题,那就是如何使用JSON序列化这些属性。