ViewModelLocator in MVVM Version 4

Jan 10, 2012 at 6:45 AM

I am writing a silverlight application where I have several views (which are user controls) that I want to use in my main page:

SO I have something like:

<UserControl x:Class="DashBoard.MainPage"
             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:local="clr-namespace:DashBoard"
             mc:Ignorable="d"
             Height="300"
             Width="300"
             DataContext="{Binding Main, Source={StaticResource Locator}}">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">     
        <!-- Link to the header item -->
        <local:Header DataContext="{Binding Header, Source={StaticResource Locator}}" Grid.Row="0"/>
    </Grid>
</UserControl>

 

Unfortunately I get an error on the <local:Header saying it is not found.  In the VewModelLocator I have added the following code to the constructor:

SimpleIoc.Default.Register<HeaderViewModel>(); 

 

To register the HEADER that I am referencing in my XAML.  I have also created method:

public HeaderViewModel Header

{

get { return ServiceLocator.Current.GetInstance<HeaderViewModel>(); }

}

 

This appears to me that it all should work, but I am not sure If I am referencing the view correctly or setting i up correctly in the new version of MvvmLight in the ViewModelLocator.  Any guidance would be greatly appreciated.

 

Thanks!

 

Michael Wade