billmaya
billmaya

Reputation: 1311

Why does attempting to show a ViewModel throw an unhandled exception?

I'm using Visual Studio 2013 and MvvmCross to create an Android application using Portable Class Libraries. When I try and load a second ViewModel based on an action in the first View, an Unhandled Exception is being thrown.

enter image description here

In my first ViewModel, CatalogViewModel, I've got an ICommand called ShowCamera that is supposed to display the second ViewModel, CameraViewModel, when a button in the CatalogView is clicked.

enter image description here

In CatalogView.axml I've bound the button to the ShowCamera method in CatalogViewModel.

enter image description here

Right now the CameraViewModel is just an empty class but it inherits from MvxViewModel just like CatalogViewModel

enter image description here

And there's also a corresponding CameraView.cs and CameraView.axml.

enter image description here

enter image description here

Has anyone else ever encountered this or have any idea why the exception is being thrown. I've tried using a try/catch in the Get to see if I can get any more specific info about the unhandled exception without any luck (the catch block is never hit, the exception is always thrown on the ShowViewModel line).

Thanks in advance.

Upvotes: 0

Views: 307

Answers (1)

billmaya
billmaya

Reputation: 1311

As Stuart pointed out it his comment, it was the lack of an [Activity] attribute on CameraView that was throwing the exception. The class definition for CameraView should be.

[Activity(Label="View for CameraViewModel")]
public class CameraView : MvxActivity
{
    ...
}

instead of

public class CameraView : MvxActivity
{
    ...
}

I initially thought about deleting the whole question out of embarrassment but decided to answer it and leave it here for anyone else who might encounter this situation in their own code.

Upvotes: 3

Related Questions