Have you every used MvvmCross in your app, only for it to crash on startup when launching without the debugger? Well it happened for me quite a few times, and its because I was having a brain fart…
After launching the app many times and trying to somehow gain knowledge magically due to repeating the same thing over and over, I decided to add some “logging”:
// Code to execute if a navigation fails private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { MessageBox.Show(e.Exception.ToString()); // <- LOGGING! if (System.Diagnostics.Debugger.IsAttached) { // A navigation has failed; break into the debugger System.Diagnostics.Debugger.Break(); } } // Code to execute on Unhandled Exceptions private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { MessageBox.Show(e.ExceptionObject.ToString()); // <- LOGGING! if (System.Diagnostics.Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger System.Diagnostics.Debugger.Break(); } }
This returns a nice little error message:
System.NullReferenceException: Object reference not set to an instance of an object. at Cirrious.CrossCore.Mvx.Resolve[TService]() at MvvmCrossSample.WindowsPhone.App.<RootFrameOnNavigating>b__0()
So what does that mean? Well, over there I am trying to resolve the IMvxAppStart
type:
RootFrame.Dispatcher.BeginInvoke(() => { Mvx.Resolve<IMvxAppStart>().Start(); });
So for some reason, my Setup object wasn’t getting initialized… This is what I had:
// Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { // ... Setup setup = new Setup(RootFrame); setup.Initialize(); }
Hmm… Maybe the app should start up for the users without debuggers as well? All I needed to do was to move the startup object out of the scope of the if
block:
// Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { // ... } Setup setup = new Setup(RootFrame); setup.Initialize();
After that, the app worked fine.
In short, don’t just copy-and-paste when using templates, at least read the snippet you are pasting.