Tuesday, August 18, 2015

MVVMCross - System.Reflection.TargetInvocationException was unhandled Message: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll

Problem:


I have my Windows phone project working great with my pcl core.  The MVVMCross libraries are working without issue.  What I have occurring is bug in the UI.  I open my app and run it just fine.  I then hit the windows button which puts the application into background.

When I reactivate the application it crashes with this error:

System.Reflection.TargetInvocationException was unhandled Message: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll

Solution:

I thought at first it would be an IoC error but it occurs to early for that.  I had to set a breakpoint in the app.xaml.cs in the Windows UI project.  The key was to set it at the Application_Activated event.
Otherwise the crash occurs and it goes straight to the dump.  I was able to walk through the issue until I hit the event RootFrame_NavigationFailed event.  Here I discovered the issue.  I took a look at the e.Exception (e is type NavigationFailedEventArgs).

e.Exception

{System.InvalidOperationException: No XAML found at the location /MainPage.xaml ...

So that was my issue!   The modifications to use MVVMCross means we do not need this page since the setup.cs file does the IoC transfer of the app at startup.  This transfer pushes control down to the PCL layer. My oversight was that the WMAppManifest.xml file has a navigation Page attribute which was set too MainPage.xaml.


NOTE to MVVMCross Users:
What I neglected to do was what Stuart Lodge points out in his N+5 series.  The key event RootFrameOnNavigation in App.xaml.cs needs to handle the IoC redirection.  I did not do this in my Windows project.  Thus the event should include the following code

Fix 1:
private void RootFrameOnNavigating(object sender, NavigatingCancelEventArgs args)
{
args.Cancel = true;
RootFrame.Navigating -= RootFrameOnNavigating;
RootFrame.Dispatcher.BeginInvoke(() => { Cirrious.CrossCore.Mvx.Resolve<Cirrious.MvvmCross.ViewModels.IMvxAppStart>().Start(); });
}

Fix 2:
I continued to have problems due to some code I dropped into the InitializePhoneApplication event.
It says don't touch very clearly!  I did anyway and this was also causing load time issues.  I eliminated my extra code and the method should look like this:

// Do not add any additional code to this method
private void InitializePhoneApplication()
{

            if (phoneApplicationInitialized)

                return;
            // Create the frame but don't set it as RootVisual yet; this allows the splash
            // screen to remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;

            // Handle navigation failures
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            // Ensure we don't initialize again
            phoneApplicationInitialized = true;
}


Now that I have done both of these steps I no longer receive the error.

Source:
  1. https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-05-MultiPage/MultiPage.Phone/App.xaml.cs
  2. http://mvvmcross.blogspot.com/

No comments:

Post a Comment