2015-11-29 81 views
2

我已經將命名空間添加到我的App.xaml文件中,以便解析項目中我的ViewModelLocator.cs位置。然後從ResourceDictionary中引用ns。但是,我得到兩個errros當我添加這些:如何解決「..dictionary項必須有關聯鍵」的錯誤?

..Each dictionary entry must have an associated key.

'ViewModelLocator' does not exist in XML namespace 'clr-namespace:MongoDBApp.ViewModels;assembly=MongoDBApp'

我檢查首先,該命名空間是用於ViewModelLocator,這是位置正確:namespace MongoDBApp.ViewModels

我也檢查了ResourceDictionary中引用的語法,看起來正確。這solution沒有解決這個錯誤,我已經清理並重建了幾次解決方案。

任何人都可以建議如何解決此錯誤?

app.xml文件的定義如下,該ResourceDictionary中接近該文件的底部:

<Application x:Class="MongoDBApp.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:converters="clr-namespace:MongoDBApp.Converters" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:local="clr-namespace:MongoDBApp.ViewModels;assembly=MongoDBApp" 
      xmlns:validators="clr-namespace:MongoDBApp.Validator" 
      StartupUri="pack://application:,,,/Views/MainView.xaml" 
      d1p1:Ignorable="d"> 
    <Application.Resources> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <DockPanel> 
          <Grid Width="16" 
            Height="16" 
            Margin="3 0 0 0" 
            VerticalAlignment="Center" 
            DockPanel.Dock="Right"> 
           <Ellipse Width="16" 
             Height="16" 
             Fill="Red" /> 
           <Ellipse Width="3" 
             Height="8" 
             Margin="0 2 0 0" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Top" 
             Fill="White" /> 
           <Ellipse Width="2" 
             Height="2" 
             Margin="0 0 0 2" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Bottom" 
             Fill="White" /> 
          </Grid> 
          <Border BorderBrush="Red" 
            BorderThickness="2" 
            CornerRadius="2"> 
           <AdornedElementPlaceholder /> 
          </Border> 
         </DockPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="Validation.HasError" Value="true"> 
        <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
     <ResourceDictionary> 
      <local:ViewModelLocator x:Key="mainViewModelLocator" ></local:ViewModelLocator> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Brown.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Brown.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary>  
    </Application.Resources> 
</Application> 
+0

嗨,我不知道它是否會幫助你,但在過去我這樣做。我創建了一個用戶控件類,派生自UserControl,然後在構造函數中設置ViewModelLocator,然後我添加的每個UserControl都將使用我的基本UserControl(因此它們都會自動獲得VML)。如果這是你認爲可以解決你的問題的東西,我可以寫一些僞代碼來說明我是如何做到的。 – adminSoftDK

+0

你可以發佈一些僞代碼來顯示該解決方案嗎? –

+1

ViewModelLocator是否公開?你編譯了它所在的程序集嗎? (設計師往往不會注意到那些尚未編譯的東西)另外,在使用合併字典時,您只能在合併字典部分內執行操作,但您有兩個可能導致錯誤的子元素。移動Merged dictionaries中的local:ViewModelLocator,查看錯誤是否消失。 (它將成爲隱式ResourceDictionary子項的一部分,然後將與其他字典聲明合併) – Maverik

回答

1

像這樣的事情應該工作,請注意我在這種情況下ViewModelLocator來自棱鏡(這就是爲什麼我需要IView,如果你使用別的東西,你不需要)。

基類

public class MyFormUserControl : UserControl, IView 
{ 
    public MyFormUserControl() 
    { 
     if (!DesignerProperties.GetIsInDesignMode(this)) 
     { 
      SetValue(ViewModelLocator.AutoWireViewModelProperty, true); 
     } 
    } 
} 

用戶控件

<controls:MyFormUserControl x:Class="MyWpf1.UserControl1" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:controls="path to the base class"> 

your usual xaml goes here 

</controls:MyFormUserControl> 

代碼背後

public partial class UserControl1: MyFormUserControl 
{ 
    public CrateFormView() : base() 
    { 
     InitializeComponent(); 
    } 
} 
相關問題