2010-06-10 89 views
1

Visual Studio 2008 Designer似乎不喜欢引用MVVM-Light ViewModelLocator的UserControls。我收到如下错误消息:可以在嵌套ViewModels中使用MVVM-Light ViewModelLocator吗?

无法创建'MyUserControl'类型的实例。

例如,如果MyUserControl使用ViewModelLocator来建立其DataContext,则以下XAML将导致此行为。

<Page x:Class="MyProject.Views.MainView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Views="clr-namespace:MyProject.Views" 
> 
    <Grid> 
     <Views:MyUserControl/> 
    </Grid> 
</Page> 

的MyUserControl是非常简单的:

<UserControl x:Class="MyProject.Views.MyUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     DataContext="{Binding MyNestedViewModel, Source={StaticResource Locator}}" 
> 
<Grid> 
    <TextBlock>Hello</TextBlock> 
</Grid> 
</UserControl> 

与“MyNestedViewModel”属性只是实例化MyNestedViewModel类,它有绝对没有代码,它的默认构造函数的一个实例。

两个问题:我用正确的ViewModelLocator

  1. 是谁?也就是说,它可以用于嵌套视图还是仅用于顶级视图?
  2. 难道这只是Cider中的另一个bug,Visual Studio 2008设计师?

请注意,在运行时,一切正常。我只在设计时遇到问题。但我讨厌编码XAML盲人。

+0

我也很想知道问题1)的答案我有一个视图与每个具有不同上下文的选项卡... – Rodney 2010-11-23 21:02:53

回答

0

我遇到VS 2010的部分解决办法我刚刚发现了同样的情况......

在你的用户控件,改变DataContextd:DataContext

<UserControl x:Class="MyProject.Views.MyUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     d:DataContext="{Binding MyNestedViewModel, Source={StaticResource Locator}}" 
> 
<Grid> 
    <TextBlock>Hello</TextBlock> 
</Grid> 
</UserControl> 

不幸的是,我不能去在用户控件YET中显示数据,只是用户控件本身。

相关问题