title: What code is executed as part of the XAML design time? In Visual Studio and Blend
body: <p>As some background, I'm playing around with a Windows Store App and Windows Phone 8 application sharing a single ViewModel (PCL).</p>
  
  <p>I'm using MVVMLight and NInject / Common Service Locator to instantiate view models and services. Which is great and gives me really nice loose coupling that is fantastic to test etc etc.</p>
  
  <p>However, getting design time data support (by switching in a different NInject module etc) is a complete black-box, and although I now have it working I'm not very happy with it, and its been mainly through trial and error.</p>
  
  <p>I'm using the standard practice of having a <code>ViewModelLocator</code> (instantiated as an app resource) that pulls View Models from the Service Locator. This always gets executed. </p>
  
  <p>Problem is, I can't tell how much of my code is actually being executed by the design-time editor. So, to ensure that NInject is initialised and the CSL hooked into NInject, I have to have a static constructor on the <code>ViewModelLocator</code> that calls into my <code>App</code> class to kick-off NInject.</p>
  
  <p>This seems wrong to me. So really, I'd like to know what the best practice for this is, and if there is actually any documentation / guarantees on which parts of the application are "started" whilst it is being shown at design time, and if that differs between Windows Store Apps and Windows Phone 8 apps.</p>
  
  <p><strong>ViewModelLocator.cs (snippet)</strong></p>
  
  <pre><code>public class ViewModelLocator
  {
      static ViewModelLocator()
      {
          System.Diagnostics.Debug.WriteLine("VML Start");
          var servicelocator = new NinjectServiceLocator(App.Kernel);
          ServiceLocator.SetLocatorProvider(() =&gt; servicelocator);
          System.Diagnostics.Debug.WriteLine("VML End");
      }
  
      public AppViewModel AppViewModel
      {
          get { return ServiceLocator.Current.GetInstance&lt;AppViewModel&gt;(); }
      }
  
      public MainViewModel MainViewModel
      {
          get { return ServiceLocator.Current.GetInstance&lt;MainViewModel&gt;(); }
      } 
  }
  </code></pre>
  
  <p><strong>App.xaml.cs (snippet)</strong></p>
  
  <pre><code>partial class App : Application
  {
      public static StandardKernel Kernel { private set; get; }
  
      static App()
      {
          // Register dependencies &amp; hook the service locator to use NInject under the hood
          var servicemodule = ViewModelBase.IsInDesignModeStatic ? (NinjectModule)new DesignTimeModule() : new RunTimeModule();
          var viewmodelmodule = new ViewModelModule();
          App.Kernel = new StandardKernel(servicemodule, viewmodelmodule);
      }
  }
  </code></pre>
  
  <p><strong>App.xaml (snippet)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Application RequestedTheme="Dark"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               x:Class="KieranBenton.LeaveNow.App.App"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:app="using:KieranBenton.LeaveNow.App"
               xmlns:dependencies="using:KieranBenton.LeaveNow.App.Dependencies"
               mc:Ignorable="d"&gt;
      &lt;Application.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  ...
              &lt;/ResourceDictionary.MergedDictionaries&gt;
              &lt;dependencies:ViewModelLocator x:Key="ViewModelLocator"
                                             d:IsDataSource="True" /&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p><strong>Main.xaml</strong></p>
  
  <pre><code>&lt;tcdcontrols:LayoutAwarePage x:Name="pageRoot"
                               x:Class="KieranBenton.LeaveNow.App.Pages.Main"
                                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                               xmlns:tcdcontrols="using:TCD.Controls"
                               xmlns:vm="using:KieranBenton.LeaveNow.ViewModel"
                               mc:Ignorable="d"
                               DataContext="{Binding MainViewModel, Source={StaticResource ViewModelLocator}}"&gt;
      &lt;Page.Resources&gt;
          &lt;!-- Collection of grouped items displayed by this page, bound to a subset of the complete item list because items in groups cannot be virtualized --&gt;
          &lt;!-- NOTE: Ridiculous lengths to get this working - see http://www.ralbu.com/post/2012/11/18/Building-WinRT-Windows-8-Grouped-items-using-MVVMLight.aspx --&gt;
          &lt;CollectionViewSource x:Name="groupedItemsViewSource"
                                Source="{Binding Journeys}"
                                d:Source="{Binding Journeys, Source={d:DesignInstance Type=vm:Main, IsDesignTimeCreatable=True}}"
                                IsSourceGrouped="true"
                                ItemsPath="Routes" /&gt;
      &lt;/Page.Resources&gt;
  </code></pre>