Search code examples
silverlight-4.0mvvm-lightchildwindowviewmodellocator

Use MVVM Light View Model Locator with Child Window in Silverlight 4


I want to use the View Model Locator in a Child Window. Problem is this don't work:

<controls:ChildWindow x:Class="Views.PopupViews.AddAlert"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:controls="clr namespace:System.Windows.Controls;assembly=System.Windows.Controls"
       DataContext="{Binding AddAlert, Source={StaticResource Locator}}>

I get the error: Cannot find a Resource with the Name/Key Locator


Solution

  • Ok the reason why it don't works is my childWindow is created inside the ctor of an IApplicationService.

    This popupService is declared in the App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator xmlns:vm="clr-namespace:Client.ViewModel" x:Key="Locator" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Assets/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    <Application.ApplicationLifetimeObjects>
        <popup:myPopupService/>
    </Application.ApplicationLifetimeObjects>
    

    Apparently, the view was created before the app resources !