如何在ReactiveCommand中实现CanExecute

时间:2018-04-16 09:31:45

标签: xamarin.forms reactive-programming reactive-property

在我们的Xamarin.Forms应用程序中,我们使用ReactiveProperty框架(https://github.com/runceel/ReactiveProperty)。

请注意,我们只使用ReactiveProperty而不是ReactiveUI。

我有一个SignInViewMode类,如下所示..

ActiveAdmin.register User do

  action_view only: :edit do
    link_to "Reset", path(id: user.id)
  end

  controller do

    def password_reset
      @user = User.find(params[id])
    end

  end
end

GoToNextCommand绑定到视图中的Button。我想实现GoToNextCommand的CanExecute功能(如果任何UserName或PhoneNumber属性为null,则禁用GoToNextCommand),但我不知道如何在ReactiveProperty中实现此功能。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

感谢您使用我的图书馆。

您可以从IObservable< bool>创建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 ... });
}
相关问题