我已经将命名空间添加到我的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>
嗨,我不知道它是否会帮助你,但在过去我这样做。我创建了一个用户控件类,派生自UserControl,然后在构造函数中设置ViewModelLocator,然后我添加的每个UserControl都将使用我的基本UserControl(因此它们都会自动获得VML)。如果这是你认为可以解决你的问题的东西,我可以写一些伪代码来说明我是如何做到的。 – adminSoftDK
你可以发布一些伪代码来显示该解决方案吗? –
ViewModelLocator是否公开?你编译了它所在的程序集吗? (设计师往往不会注意到那些尚未编译的东西)另外,在使用合并字典时,您只能在合并字典部分内执行操作,但您有两个可能导致错误的子元素。移动Merged dictionaries中的local:ViewModelLocator,查看错误是否消失。 (它将成为隐式ResourceDictionary子项的一部分,然后将与其他字典声明合并) – Maverik