Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveUI - Confused about Routing

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.

like image 356
Steve Macdonald Avatar asked Jan 17 '14 02:01

Steve Macdonald


1 Answers

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:

  • https://github.com/reactiveui/ReactiveUI/pull/391 - Pull Request / Progress
  • https://github.com/reactiveui/ReactiveUI/tree/docs/docs/basics - Browsable version
like image 161
Ana Betts Avatar answered Sep 21 '22 14:09

Ana Betts