Design time data for WinRT issues - object reference not set to an instance of an object

Topics: UI Architecture
Jul 7, 2013 at 5:20 AM
I've been having issues getting this to work. I'm trying to set a design time data context, and I've referred to numerous examples on various blogposts but still no luck.

Here is a section from my view
    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
Jul 7, 2013 at 11:13 PM
show us your config method of your bootstrapper...
Jul 12, 2013 at 7:54 AM
Hi there,
here is my app.xaml.cs
namespace MyApp
{
  sealed partial class App
    {
        // add our IOC container for registering services etc
        private WinRTContainer container;

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        protected override void Configure()
        {
            container = new WinRTContainer();
            container.RegisterWinRTServices();
            container.PerRequest<NewsViewModel>();
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }

        protected override void PrepareViewFirst(Frame rootFrame)
        {
            container.RegisterNavigationService(rootFrame);
            container.RegisterSingleton(typeof(INewsRepository), "NewsRepository", typeof(NewsDataSource));
        }

        protected override async void OnLaunched(LaunchActivatedEventArgs args)
        {
            DisplayRootView<NewsView>();
        }
}
Jul 12, 2013 at 9:27 AM
OK, I've found the issue. It was all my fault. In my design time view model, I was trying to add to a List without initialising it first. by going
private List<NewsItem> _itemListView = new List<NewsItem>();
        public List<NewsItem> ItemListView
        {
            get { return _itemListView; }
            set { _itemListView = value; }             
        }

noobmistake

Otherwise, the technique I used to set up design time data works.

The lesson I learned here is, it's hard to tell if there is something going wrong in your view model when all it says is "object not set to an instance", but if that is the error message you get, always make sure to check your own view model for errors.