0

Having got IntelliSense for Data Binding working in a simple test application, thanks to the answer to my previously raised question, I'm now trying to apply what I've learnt to the actual application I'm working on. Again I'm encountering problems that I don't understand. A snippet of my code is below - I've had to change names to protect propriety information:

<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>

I'm getting an error message on the line <StackPanel d:DataContext="{d:DesignInstance ViewModels:MyViewModel}">:

The name "MyViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels".

The error doesn't make sense MyViewModel does exist within the MyProject.ViewModels namspace. Any suggestions? I've tried a clean rebuild.

Community
  • 1
  • 1
Dan Stevens
  • 6,392
  • 10
  • 49
  • 68

2 Answers2

1

The MyProject.ViewModels namepsace is within a different assembly to the MyProject.Views.Pages and it appears to necessary to add ;assembly=MyProject.ViewModels to the xmlns:ViewModels="clr-namespace:MyProject.ViewModels delcaration:

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

I assumed that because the assembly is referenced by the project, I wouldn't need to specify an assembly, just as I don't have to specify an assembly when using a namespace within a C# code file.

Dan Stevens
  • 6,392
  • 10
  • 49
  • 68
0

I just got the same error and I got it many times before. Each time I have this error, it come from the fact that I have other errors and my code won't compile for other reasons. As soon as I fixed all other errors, my code compile and VisualStudio find all 'd:' missing references... if they really exists.

Also, as an alternative, if you instanciate your ViewModel with its default constructor, I suggest to use something like (without using 'd:'):

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

It will solve your problem forever.

I haven't tried it yet, but perhaps that with Roslyn (the new VS2015 compiler) this problem will go away. I hope :-)

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119