xamarin - ReactiveUI Xamarin iOS 路由

标签 xamarin xamarin.ios routing reactiveui

我开始在 Xamarin iOS 中尝试响应式 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);
    }
}

和 ViewDidLoad( Controller ):

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);
});

这有效地绑定(bind)了那些 UITextFields 和登录按钮启用状态的值 + OnTouchUpInside

现在,在德documentation你可以找到以下关于 vm-routing 的内容:Android, iOS Native: Very difficult to make work

那么我的选择是什么?

公开一个 DidLogIn 属性(bool)并监听(在 View 中):

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

是否有其他方法来处理 View 路由(不是 vm 路由),我能找到的相关信息很少

最佳答案

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
                {}
        });
    }
}

所以在你看来

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

该示例并不完美,但它是一种实现方式。

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

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

问候

关于xamarin - ReactiveUI Xamarin iOS 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50527773/

相关文章:

asp.net-mvc - ASP.NET MVC-生成没有Http/请求上下文的路由

ruby-on-rails - rails ajax 调用是否应该捆绑到它们自己的单独 Controller 中?

mysql - 将 Xamarin.forms 与 MySQL 连接

azure - 将 Azure 移动应用程序与 Azure Web 应用程序(包括身份验证)相结合

xamarin - 为什么我会收到警告NU1605,它以蓝色显示

c# - Xamarin.iOS 中带有填充的 UILabel?

c# - 更新后缺少iOS模拟器列表

ios - MonoTouch通用限制和iOS模拟器

ios - 绑定(bind) Objective-C 包时出错

vue.js - 子路由组件不在 vue js 中呈现