Search code examples
xamlwinrt-xamlcaliburn.microdesign-timedesign-time-data

Caliburn.micro design time data for WinRT - object reference not set to an instance of an object


I'm doing windows 8 app dev using the caliburn.micro MVVM framework.

I'm having issues with design time data. I've looked high and low through various blogs and what not to find an answer. No luck so far.

Here is a section from my view where I say use this view model for design time

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="using:Caliburn.Micro"  
xmlns:vm="using:MyApp.SampleViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:SampleNewsViewModel, IsDesignTimeCreatable=True}"
cal:Bind.AtDesignTime="True">

The d:DataContext... part is being underlined and saying "object reference not set to an instance of an object"

I have a view model with a default constructor

namespace MyApp.SampleViewModels
{
   public sealed class SampleNewsViewModel 
   {
       public SampleNewsViewModel()
       {
           Title = "News Title";
       }

       private string _title;
       public string Title
       {
           get { return _title; }
           set { _title = value; }
       }
   }
}

Pretty sure there's nothing wrong with my ViewModel (but I could be wrong). I can't figure this out, any point in the right direction would be awesome.

cheers, Lochana


Solution

  • Oh man...I found the issue, and it's my fault.

    In my design time view model, I had not initialized the list, and in the constructor, was trying to add items to it.

    This fixed it

    private List<NewsItem> _itemListView = new List<NewsItem>();
        public List<NewsItem> ItemListView
        {
            get { return _itemListView; }
            set { _itemListView = value; }             
        }
    

    The lesson I learned here is that the error message "Object reference not set to an instance of an object" can mean you're view model is broken, even though it doesn't explicitly say. So for anyone starting out with caliburn.micro, unit test your design time view models to make sure they work as expected.