2011-08-17 93 views
2

我想在子窗口中使用View Model Locator。 問題是這樣不起作用:在Silverlight 4中使用帶有子窗口的MVVM Light View模型定位器

<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}}> 

我得到的錯誤: 無法找到名稱/密鑰定位

+0

你最後失蹤「」。我修好了,試試看:DataContext =「{Binding AddAlert,Source = {StaticResource Locator}}」> – Rumplin

回答

1

好的原因是我的childWindow是內部創建IApplicationService的ctor。

這popupService在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> 

顯然,創建視圖的應用程序資源以前

1

資源沒有招一個子窗口綁定到一個靜態使用定位器模式查看模型。我的猜測是你的DataContext是錯誤的。

檢查: 確保您的定位器類中定義了「AddAlert」屬性。喜歡的東西:

private static AddAlertViewModel _AddAlertViewModel; 

    /// <summary> 
    /// Gets the ViewModelPropertyName property. 
    /// </summary> 
    public static AddAlertViewModel AddAlertViewModelStatic 
    { 
     get 
     { 
      if (_AddAlertViewModel == null) 
      { 
       CreateAddAlertViewModel(); 
      } 

      return _AddAlertViewModel; 
     } 
    } 

    /// <summary> 
    /// THIS PROPERTY IS WHAT YOU NEED TO REFERENCE IN YOUR XAML 
    /// </summary> 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")] 
    public AddAlertViewModel AddAlert 
    { 
     get 
     { 
      return AddAlertViewModelStatic; 
     } 
    } 

當然,請確保您的視圖模型定位在你App.xaml文件實例:

爲什麼它不工作
<vm:MyModelLocator xmlns:vm="clr-namespace:MyAppNamespace" x:Key="Locator" /> 
相關問題