0
votes

I'm having a strange issue, and I can't seem to find any answers on.

I'm building a Metro Store App with Visual Studio 2012 using MVVM Light through NuGet.

When designing and adding the data context xaml code, studio gives me the red swiggly with the error "Object reference not set to an instance of an object".

DataContext={Binding MainVM, Source={StaticResource Locator}}

I finally narrowed it down to the inheritance in my viewmodel. I have a base viewmodel that all my viewmodels inherit from declared as such:

public class BaseViewModel : ViewModelBase
{
}

Then obviously all my viewmodels as such:

public class MainViewModel : BaseViewModel
{
}

Now, at runtime everything works perfectly fine (which is confusing to me), but the design mode breaks.

However, if I remove the inheritance and have my viewmodels directly inherit the ViewModelBase, everything is fine in design mode:

public class MainViewModel : ViewModelBase
{
}

I thought that perhaps something in the code was causing an issue, so I commented everything out except what was required to compile and still receive the same result.

Anyone seem to have this issue, or know what I might be doing wrong by this? I normally use this same pattern when working on Silverlight or WPF applications using MVVM and things seem fine there.

Just a sidenote, my ViewModelLocator does have a MainVM property that returns the MainViewModel class.

UPDATE After reading LBugnion and Will's comments I was getting ready to debug the design mode when I noticed I goofily had forgotten to comment out my code in the BaseViewModel. I did find that there was breaking code in there. Design mode did not like the following line:

private CoreDispatcher UIDispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

Which I was using to update the UI thread in code. I'm guessing (which someone more experienced may chime in) there is no UI thread in design mode?

Normally I use the "IsInDesignMode" property to make my viewmodels almost non-functional for design, but apparently forgot to do so here, so I changed the line above to

private CoreDispatcher UIDispatcher = IsInDesignModeStatic ? null : Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

My goof

1
I do that routinely so normally this alone shouldn't break design mode. There must be an exception thrown somewhere, did you try to debug the design mode code?LBugnion
Yes exactly, I normally do the same with the other technologies as well, so I was thinking it might have been a Metro thing. Apparently I was wrong and I goofed up my debugging (as stated in the update above) so thankfully everything is working fine now. Thanks!Esteban Martinez

1 Answers

2
votes

Happens all the time, mostly because code in your types is being executed in the designer.

Yes, code can actually execute in the designer. The designer will load your assembly into the AppDomain for the design surface, instantiate your type, and use that instance against the design surface.

Consequently, if you have something like this:

public MyView : UIElement
{
    public MyView()
    {
        InitializeComponent();
        var database = GetDatabaseObject();
        database.OpenLikeWeAreInProduction();
        FillTheUiLol(database.GetAllKindsOfCrap());
    }
}

it's most likely going to break as your database doesn't exist, you can't find the connection string from app.config, etc etc etc.

The way to handle this is that, whenever you have code which might execute in the designer (such as in constructors, in property change event handlers, etc), you should be using DesignerProperties.GetInDesignMode(new DependencyObject()) to determine if you are in the designer before executing code guaranteed to break in the designer.

Unfortunately, sometimes its very hard to determine what code is breaking. The only solution I have for the hard-to-deduce failures is to note the exception type, spin up another instance of Visual Studio, attach to the first, then configure debugging to always break for that exception type.