2014-11-02 100 views
0

我學習創建下面這個tutorial的MVVM應用程序,它有這個在它的入口視圖時WPF MVVM錯誤:有關查看和視圖模型

什麼的例子

<UserControl.Resources> 
    <DataTemplate DataType="{x:Type vm:ProductViewModel}"> 
     <vw:ProductView /> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type vm:SingleBrandViewModel}"> 
     <vw:SingleBrandView /> 
    </DataTemplate> 
</UserControl.Resources> 

工作所以我想在我的代碼如下

什麼也沒有在我的代碼工作

<Page x:Class="MvvmAttempt.FilesView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:MvvmAttempt" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
    Title="FilesView"> 

    <Page.Resources> 
     <ResourceDictionary> 

      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="resources/Styles.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
      <local:MySizeConverter x:Key="sizeConverter"/> 

      <DataTemplate DataType="{x:Type local:SingleFileView}"> <--- error here 
       <local:SingleFileViewModel/> <--- error here 
      </DataTemplate> 

     </ResourceDictionary> 
    </Page.Resources> 
    ... other stuff ... 
</Page> 

x:Type有下劃線的錯誤消息:The type 'x:Type' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

<local:SingleFileViewModel/>下,誤差爲:The specified value cannot be assigned. The following type was expected: "DependencyObject".

什麼引起的錯誤?爲什麼在示例代碼中沒有任何內容時期望DependencyObject?謝謝!

回答

1

可能是你的錯誤就坐落在這裏:

<DataTemplate DataType="{x:Type local:SingleFileView}"> <--- error here 
    <local:SingleFileViewModel/> <--- error here 
</DataTemplate> 

請注意,您所指定的視圖模型爲DataTemplate中,並查看DataTemplate中的數據類型。所以在我看來,這應該工作:

<DataTemplate DataType="{x:Type local:SingleFileViewModel}"> 
    <local:SingleFileView/> 
</DataTemplate> 
+0

謝謝!現在'x:Type'這行仍然存在抱怨,但它編譯得很好。所以猜我會忽略它... – totoro 2014-11-02 07:51:45