As some background, I play with a Windows Store app application and Windows Phone 8 sharing the same ViewModel (PCL).
I use MVVMLight and NInject / Common Service Locator to instantiate models and services. This is great and gives me a really nice bunch that is great for testing, etc. Etc.
However, getting information about development time (by switching to another NInject module, etc.) is a complete black box, and although I now have work, I am not very happy with this, and this was mainly through a trial version and an error.
I use standard usage practice ViewModelLocator(created as an application resource) that pulls View Models from Locator. This is always done.
The problem is that I can’t say how much of my code is actually executed by the development time editor. So, in order to initialize NInject and connect the CSL to NInject, I have to have a static constructor in ViewModelLocatorwhich calls my class Appto run NInject.
This seems to me wrong. Therefore, I would like to know what is best suited for, and if there really is any documentation / guarantees, which parts of the application are “running” while they are displayed during development, and if this differs from the Windows Store application and the application for Windows Phone 8.
ViewModelLocator.cs (snippet)
public class ViewModelLocator
{
static ViewModelLocator()
{
System.Diagnostics.Debug.WriteLine("VML Start");
var servicelocator = new NinjectServiceLocator(App.Kernel);
ServiceLocator.SetLocatorProvider(() => servicelocator);
System.Diagnostics.Debug.WriteLine("VML End");
}
public AppViewModel AppViewModel
{
get { return ServiceLocator.Current.GetInstance<AppViewModel>(); }
}
public MainViewModel MainViewModel
{
get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
}
}
App.xaml.cs (snippet)
partial class App : Application
{
public static StandardKernel Kernel { private set; get; }
static App()
{
var servicemodule = ViewModelBase.IsInDesignModeStatic ? (NinjectModule)new DesignTimeModule() : new RunTimeModule();
var viewmodelmodule = new ViewModelModule();
App.Kernel = new StandardKernel(servicemodule, viewmodelmodule);
}
}
App.xaml(snippet)
<?xml version="1.0" encoding="utf-8"?>
<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">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
<dependencies:ViewModelLocator x:Key="ViewModelLocator"
d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
</Application>
Main.xaml
<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}}">
<Page.Resources>
<CollectionViewSource x:Name="groupedItemsViewSource"
Source="{Binding Journeys}"
d:Source="{Binding Journeys, Source={d:DesignInstance Type=vm:Main, IsDesignTimeCreatable=True}}"
IsSourceGrouped="true"
ItemsPath="Routes" />
</Page.Resources>