ReactiveUI - Confused about Routing
Asked Answered
L

1

8

I'm in the process of comparing MvvmCross with ReactiveUI for a major pharma project on Win Store, WP8, iOS, Droid. We've already selected Xamarin.

I'm totally new to ReactiveUI. I really like what I see in principle, and I think Paul is a genius. However the details are becoming a real bear. I've spend several days tracking down documentation (the manual is from 2011 and seems almost entirely outdated - it doesn't even contain the word "Router") and sample code.

I'm looking at the sample from ReactiveUI.Samples Also the project MobileSample-RT from the ReactiveUI solution.

I based my little hello world on the ReactiveUI.Samples "Routing" example. Frankly this isn't much of an example as all it does is navigate from AppBootstrapper to the one and only view. I'm trying to do something similar to the "three page" example from MobileSample-RT. The problem is, if I try something like this in my project:

HostScreen.Router.Navigate.Execute(RxApp.DependencyResolver.GetService(typeof(LoginViewModel)));

It crashes (pdb symbols not loaded in ReactiveUI.dll)

If I try this:

HostScreen.Router.NavigateCommandFor<LoginViewModel>().Execute(HostScreen);

Same result - hard crash. That really threw me as it seems like something that should "just work".

I can call this:

HostScreen.Router.Navigate.Execute(new LoginViewModel(HostScreen));

And it does go to my view, as expected. I also wired up a back button in the main screen:

this.OneWayBind(AppBootstrapper, x => x.Router.NavigateBack, x => x.BackButton.Command);

And that indeed moves back from the view to which I'd just navigated.

So now I want to move forward again. I click on the button that does this (again):

HostScreen.Router.Navigate.Execute(new LoginViewModel(HostScreen));

And I go back to that view. However this time it takes 2 clicks on the back button to get actually move back. If I then move forward again, it takes 3 clicks the next time. The NavigationStack is filling up with new instances of LoginViewModel.

So, what IS the correct way to do routing/navigation? Why are those commands crashing? Rather than calling "new" each time in the Navigate.Execute, how do I navigate to a viewmodel that is already in the navigation stack (or should it be there in the first place?).

Many thanks for any clarity you can provide.

Loveridge answered 17/1, 2014 at 2:41 Comment(1)
You mentioned the pdb not loaded. Paul pushes every build to symbolsource.org, and I have found configuring that to be invaluable in understanding and learning reactiveui. Instructions on how to configure visual studio debugger are on the site.Cointon
P
9

If you're building for iOS and Android, you simply don't want to use Routing - the notion of routing conflicts too much with what the platforms want you to do (even on WP8 it's a stretch but I was able to hacky make it work). You should stick with View-first location.

However, if you're using Xamarin Forms, ReactiveUI works great with VM-based routing (since Xamarin managed to do all the hacks to make it possible!). Check out https://github.com/paulcbetts/xamarinevolve2014 for a demo of it

I know the docs suck and I'm working on new ones:

Paulapauldron answered 18/1, 2014 at 19:43 Comment(1)
@PaulBetts Do you still recommend to avoid Routing for Xamarin.ios and Xamarin.Android ? We are thinking using MVVMCross for the navigation via viewmodel and ReactiveUI + Reactive extensions for the plumbing of the application. Do you see any issue about this configuration ? Or there a way that i don't see to navigate via viewmodel with ReactiveUI ?Eunuchoidism

© 2022 - 2024 — McMap. All rights reserved.