“找不到名称为{Locator}的资源";使用mvvm-light用户控件时出错 [英] "Resource with the name {Locator} cannot be found" Error when using mvvm-light user control
问题描述
我正在使用mvvm light工具包来创建WPF应用程序.我创建了一个用户控件和一个对应的ViewModel.我在ViewModelLocator中创建了一个ViewModel属性.我将用户控件datacontext绑定到Locator类中的属性.当我在Blend或VS Designer中编辑用户控件时,一切似乎都可以正常工作,因为我可以看到我的设计时间数据.
i am using the mvvm light toolkit to create a WPF application. I created a user control and a corresponding ViewModel. I created a ViewModel property in the ViewModelLocator. I bound the user controls datacontext to the property in the Locator class. When i edit the User Control in Blend or the VS Designer everything seems to work, since i can see my design time data.
当我现在尝试在由工具箱的wpf模板创建的主窗口上使用我的用户控件时,我收到错误消息找不到名称为{Locator}的资源",并且出现了我的用户控件所在的行在Blend中,在mainwindow.xaml中用红线标记.在Visual Studio中,同一行标记有错误:无法创建MyView类型的实例".
When i now try to use my user control on the main window, which is created by the wpf template of the toolkit i receive the error "Resource with the name {Locator} cannot be found" and the row with my user control in the mainwindow.xaml is marked with a red line in Blend. In Visual Studio the same line is marked with the error: "Cannot create an instance of type MyView".
这是app.xaml的源代码
This is the source code of the app.xaml
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True">
</Application.Resources>
这是EditCustomerView.xaml的代码
This is the code of the EditCustomerView.xaml
<UserControl.DataContext>
<Binding Path="EditCustomer" Source="{StaticResource Locator}" />
</UserControl.DataContext>
这是我主窗口中的代码
This is the code in my main Window
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>
<Grid x:Name="LayoutRoot" Background="{DynamicResource BasicBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="0.927*"/>
<RowDefinition Height="0.073*"/>
</Grid.RowDefinitions>
<ListBox Margin="4" SelectedItem="{Binding Main.SelectedCustomer, Mode=Default, Source={StaticResource Locator}}" ItemTemplate="{DynamicResource CustomerTemplate}" ItemsSource="{Binding Customers, Mode=Default}"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Content="Edit" Grid.Row="1" Command="{Binding EditCustomerCommand, Mode=Default}"/>
<Border x:Name="border" Opacity="0.75" Grid.RowSpan="2" Background="#FF706F6F" BorderBrush="Black" BorderThickness="1" Visibility="{Binding EditViewVisibility, Mode=Default}">
<views:EditCustomerView HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
应用程序编译并运行.该错误仅在设计时发生.
The application compiles and runs. The error is only occuring during design time.
你能告诉我我在做什么错吗?
Can you tell me what i am doing wrong?
谢谢.
推荐答案
这是一个已知问题.由于某种原因,Blend无法识别静态全局资源.
This is a known issue. Blend for some reason doesn't recognize the static global resource.
作为解决方法,您可以在视图中创建ViewModelLocator的本地资源.
As a workaround you can create a local resource of ViewModelLocator in your Views.
<Window.Resources>
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True">
</Window.Resources>
您必须包含ViewModel命名空间.
You have to include the ViewModel namespace.
似乎在Blend 4中已解决
Seems it is resolved in Blend 4
这篇关于“找不到名称为{Locator}的资源";使用mvvm-light用户控件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!