2010-06-10 87 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中顯示數據,只是用戶控件本身。

相關問題