Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

First off, make sure you have got the namespaces configured correctly (this gives me a headache many times). Now with that out of our way, i can tell you to try too things:

  1. Try to add these to your namespace declarations mc:Ignorable="d" and xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006".

  2. If this alone didn't work then try to replace Type=vm:SampleNewsViewModel with Type={x:Type vm:SampleNewsViewModel and see if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top