ReactiveUI Xamarin iOS路由

RVa*_*een 3 routing xamarin.ios reactiveui xamarin

我开始在Xamarin iOS中尝试使用Reactive UI,但是我对应该如何处理路由感到困惑。

让我们以典型的“ LoginViewModel”为例:

public class LoginViewModel : ReactiveObject
{
    private readonly ReactiveCommand loginCommand;
    public ReactiveCommand LoginCommand => this.loginCommand;

    string username;
    public string Username
    {
        get { return username; }
        set { this.RaiseAndSetIfChanged(ref username, value); }
    }

    string password;
    public string Password
    {
        get { return password; }
        set { this.RaiseAndSetIfChanged(ref password, value); }
    }

    public LoginViewModel()
    {
        var canLogin = this.WhenAnyValue(
            x => x.Username,
            x => x.Password,
            (u, p) => !string.IsNullOrEmpty(u) && !string.IsNullOrEmpty(p)
        );

        this.loginCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            //Simulate login
            await Task.Delay(2000);
            return true;
        }, canLogin);
    }
}
Run Code Online (Sandbox Code Playgroud)

和ViewDidLoad(控制器):

this.WhenActivated(d =>
{
    this.Bind(this.ViewModel, x => x.Username, x => x.Username.Text).DisposeWith(d);
    this.Bind(this.ViewModel, x => x.Password, x => x.Password.Text).DisposeWith(d);
    this.BindCommand(this.ViewModel, x => x.LoginCommand, x => x.LoginButton).DisposeWith(d);
});
Run Code Online (Sandbox Code Playgroud)

这有效地绑定了那些UITextFields和Login按钮启用状态+ OnTouchUpInside的值

现在,在文档中,您可以找到有关vm-routing的以下信息:Android,iOS Native:很难工作

那么我在这里有什么选择呢?

公开一个DidLogIn属性(布尔),并使用以下方法监听(在视图中):

this.WhenAnyValue(x => x.ViewModel.DidLogIn)
    .ObserveOn(RxApp.MainThreadScheduler)
    .Subscribe(() => {
        //Routing logic here
    });
Run Code Online (Sandbox Code Playgroud)

还有其他方法来处理视图路由(不是vm-routing),我几乎找不到关于此的信息

Adr*_*ero 5

ReactiveUI在Xamarin Forms上的路由非常简单,Xamarin Android / iOS是不同的历史记录,但是您可以在以下示例中尝试ReactiveUI的交互:

public class LoginViewModel : ReactiveObject
{

    private readonly Interaction<Unit, Unit> _navigate;
    public Interaction<Unit, Unit> Navigate => Navigate;

    public ReactiveCommand<Unit,bool> LoginCommand { get; set; }


    public LoginViewModel()
    {
        var canLogin = this.WhenAnyValue(
        x => x.Username,
        x => x.Password,
        (u, p) => !string.IsNullOrEmpty(u) && !string.IsNullOrEmpty(p));

        _navigate = new Interaction<Unit, Unit>();

        LoginCommand = ReactiveCommand.CreateFromTask<Unit, bool>(async _ =>
        {
            /*Logic here*/
            return true;
        }, canLogin);

       LoginCommand.Subscribe(async result => 
        {
            if (result)//this logic gets executed on your view by registering a handler :D
                await await _navigate.Handle(Unit.Default) ;
            else
                {}
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

所以在你看来

this.WhenActivated(disposables =>
{
   //bindings...
   //Register a handler:
   ViewModel.Navigate.RegisterHandler(async interaction =>
   {
       await NavigationLogic();
       interaction.SetOutput(Unit.Default);
   }).DisposeWith(disposables);
});
Run Code Online (Sandbox Code Playgroud)

这个例子并不完美,但是是做到这一点的一种方法。

希望对您有所帮助,您可以在以下网址找到有关互动的更多信息:https : //reactiveui.net/docs/handbook/interactions/

Xamarin Android + ReactiveUI中也有一个示例:https : //github.com/bl8/ReactivePhoneword

问候