0
votes

I'm testing out MvvmCross in an existing Monotouch project. I would like to start migrating the project to Mvvm in pieces.

I can start the application and it shows my first (mvvm) screen. After that, i'm doing different (non mvvmcross) things (loading views etc) which I would like to port on a later moment.

I was now trying to port one of my views also to mvvmcross. I created a View "public class TestView : MvxViewController" and a ViewModel "public class TestViewModel : MvxViewModel"

When loading that View, the app crashed with following error:

---Message:
Object reference not set to an instance of an object
---Stack Trace:
  at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.LoadViewModel (IMvxTouchView touchView) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods+<OnViewCreate>c__AnonStorey3.<>m__9 () [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (IMvxView view, System.Func`1 viewModelLoader) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.OnViewCreate (IMvxTouchView touchView) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void__this___object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Touch.Views.MvxDelegateExtensionMethods.Raise (System.EventHandler eventHandler, System.Object sender) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.Touch.Views.MvxEventSourceViewController.ViewDidLoad () [0x00000] in <filename unknown>:0 
  at CatalogMVVMTest.Touch.Views.TestView.ViewDidLoad () [0x0001a] in /Users/matthiasvalcke/Projects/MVVMDemos/CatalogMVVMTest/CatalogMVVMTest.Touch/Views/TestView.cs:20

...

It seems it crashes when calling "base.ViewDidLoad ();"

Not sure if this is important, but after loading the first view, I'm loading a new (non mvvmcross) view as root of the navigation stack.

So now I'm trying to load a MvvmCross View to a normal UIView(Controller)

I'm also getting following Mvx diagnostic output:

mvx: Diagnostic:   1.08 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction

edit:

I'm also getting it if I just edit a simple sample from MvvmCross. For example, if I just take the basic sample with one "FirstView" and "FirstViewModel" and I do something like this, I'm getting the error:

var secondv = new SecondView ();
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);

SecondView is just a MvxViewController. It seems like I can not add a View from a MvxViewController to another MvxViewController in code. Is this correct?

Anyone having an idea?

2

2 Answers

3
votes

Ok, instead of doing this:

var secondv = new SecondView (); //(this is a MvxViewController
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);

I had to do this (i found it in the tabbar sample):

var viewModel = new SecondViewModel ();
var secondv = this.CreateViewControllerFor(viewModel) as UIViewController;
secondv.View.Frame = new RectangleF (0,250,320,10);
secondv.View.AutoresizingMask = UIViewAutoresizing.None; //was needed because mvvm is enabling autostretch automatically? 
Add (secondv.View);

Sorry for the questions, I'm using a lot of UIViewControllers on other UIViewControllers and it's difficult to understand the complete MvvmCross framework :-)

I'm still getting following diagnostic output, but no nullpointer anymore and it works:

mvx: Diagnostic:   0.11 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction

Thanks for the help!

0
votes

You are most likely hitting a bug in Xamarin's latest release.

This was addressed in https://github.com/slodge/MvvmCross/commit/5786d4964e4262cbf91661f04427e19648acf7df

Try upgrading to the mvvmcross 3.0.11-beta1 release or later - or roll back your xamarin version.

More info on http://forums.xamarin.com/discussion/comment/26049/#Comment_26049