2015-04-02 79 views
0

在一個簡單的測試應用程序中獲得了用於數據綁定的智能感知,感謝我的previously raised question的答案,我現在試圖將我所學到的知識應用於實際應用程序中,正在努力。我又遇到了我不明白的問題。我的代碼片段低於 - 我不得不改變名稱,以保護可否信息:數據綁定智能感知不起作用 - 後續工作

名稱「MyViewModel」:

<Page x:Class="MyProject.Views.Pages.MyPage" 
     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" 
     mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="450" 
     xmlns:Converters="clr-namespace:MyProject.Converters" 
     xmlns:ViewModels="clr-namespace:MyProject.ViewModels" 
     Title="My View" 
     SnapsToDevicePixels="True" KeepAlive="True" TextOptions.TextFormattingMode="Display"> 
    <Page.Resources> 
     <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
     <Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/> 
    </Page.Resources> 

    <StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}"> 
     <!-- ... --> 
    </StackPanel> 
</Page> 

我就行了<StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">收到錯誤消息不存在於名稱空間「clr-namespace:MyProject.ViewModels」中。

錯誤沒有意義MyViewModel確實存在於MyProject.ViewModels namspace中。 有什麼建議嗎?我試過了一個乾淨的重建。

回答

1

MyProject.ViewModels namepsace是不同的裝配到MyProject.Views.Pages內,它似乎需要添加;assembly=MyProject.ViewModelsxmlns:ViewModels="clr-namespace:MyProject.ViewModels delcaration:

xmlns:ViewModels="clr-namespace:MyProject.ViewModels;assembly=MyProject.ViewModels" 

我認爲,因爲組件由該項目引用,我不會」不需要指定一個程序集,就像在C#代碼文件中使用名稱空間時不必指定程序集一樣。

0

我剛剛得到了同樣的錯誤,我以前多次得到它。每次出現此錯誤時,都會出現其他錯誤,並且由於其他原因我的代碼無法編譯。只要我修復了所有其他錯誤,我的代碼編譯和VisualStudio就會找到所有'd:'缺少的引用......如果它們真的存在。

此外,作爲替代方案,如果你使用它的默認構造函數實例化的視圖模型,我建議使用類似(不使用「d:」):

... 
</Page> 
<Page.DataContext> 
    <ViewModels:MyViewModel/> 
</Page.DataContext> 

它將永遠解決您的問題。

我還沒有嘗試過,但也許與Roslyn(新的VS2015編譯器),這個問題將消失。我希望:-)