Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反应UI Xamarin iOS路由_Xamarin_Xamarin.ios_Routing_Reactiveui - Fatal编程技术网

反应UI Xamarin iOS路由

反应UI Xamarin iOS路由,xamarin,xamarin.ios,routing,reactiveui,Xamarin,Xamarin.ios,Routing,Reactiveui,我开始在Xamarin iOS中试验反应式UI,但我不知道该如何处理路由 让我们以典型的“LoginViewModel”为例,它有如下内容: public class LoginViewModel : ReactiveObject { private readonly ReactiveCommand loginCommand; public ReactiveCommand LoginCommand => this.loginCommand; string user

我开始在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(控制器):

这将有效地绑定那些UITextFields和Login button enable states+onTouchInside的值

现在,在de中,您可以找到以下关于vm路由的信息:Android,iOS本机:很难正常工作

那么我的选择是什么呢

公开一个DidLogIn属性(bool)并在该属性上侦听(在视图中):

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

是否有其他方法来处理视图路由(而不是vm路由),我可以找到关于这方面的信息非常少

在Xamarin表单上的ReactiveUI路由非常简单,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
                {}
        });
    }
}
这个例子并不完美,但却是一种方法

我希望这对您有所帮助,您可以在以下网站上找到更多关于互动的信息:

Xamarin Android+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);
});