0

I have a BIG and OLD xaml user control and I am trying to add ViewModel class for it (to do the things right). the code for attach viewmodel in xaml is:

xmlns:vm="clr-namespace:Company.Proj.ViewModel;assembly=Company.Proj"
             xmlns:v="clr-namespace:Company.Proj.View;assembly=Company.Proj"
             mc:Ignorable="d">
    <UserControl.Resources>
        <DataTemplate
            DataType="{x:Type vm:SampleVM}">
            <v:MainWindow/>
        </DataTemplate>
    </UserControl.Resources>
    <UserControl.DataContext>
        <vm:SampleVM/>
    </UserControl.DataContext>

and a class for future ViewModel:

namespace Company.Proj.ViewModel //the namespace is correct
{
    public class SampleVM    // the class is public
    {
        public SampleVM() // void constructor? perhaps not needed here
        {

        }
    }
}

And I am getting SampleVM does not exist in the namespace clr-namespace:Company.Proj.ViewModel;assembly=Company.Proj"

I have googled a lot and found out many answers. like in The name ViewModel does not exist in the namespace "clr-namespace:Project.ViewModels"

...I have tried to rebuildProjec/reopenStudio/change debug-release-debug/carefully)) copypaste assembly and namespece names...

But nothing solved (( Is there any other variant to connect ViewModel to View.xaml(perhaps the better one that I have used) OR what coul be done to solve this issue ?

My VS version: VS Community 2013 12.0.3.1101.00 Update 4 Target Framework 4.5.1

EDIT: My question is different from above link because In my question there is no mistake in clr-path (see top answer of Will)

Community
  • 1
  • 1
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • Is the assembly correct? If the `UserControl` is in the same assembly as the viewmodel, this shouldn't even be required. – Charles Mager May 21 '15 at 13:37
  • Yes.. Buy the way assembly is not necessary here(one project) - I have also tried xmlns:vm="clr-namespace:Company.Proj.ViewModel without assembly. The same error( – curiousity May 21 '15 at 13:40
  • The problem is not finding SampleVM class, but the code is unable to find the namespace (or assembly) somehow. – Marshal May 21 '15 at 13:43
  • Yes - the SampleVM is public class (take a look at my code) all this classes are from ONE assembly – curiousity May 21 '15 at 13:44
  • possible duplicate of [The name ViewModel does not exist in the namespace "clr-namespace:Project.ViewModels"](http://stackoverflow.com/questions/17025601/the-name-viewmodel-does-not-exist-in-the-namespace-clr-namespaceproject-viewmo) – Marshal May 21 '15 at 14:27
  • remove x:type, – Usman Ali Jan 10 '18 at 10:46

1 Answers1

0

To debug it simply, you may try following steps:

  1. Take a new project.
  2. Add assembly Company.Proj.dll as a reference to it.
  3. Try to map the viewmodel namespace (or any other namespace) with xmlns:prefix directive.

PS: As you asked in the question, void constructor is not a problem. It needs a constructor to get initialized. Classes without constructor cannot be initialized (except using factorymethods).

Edit: You may like to see answers on a similar question.

Community
  • 1
  • 1
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • I have add reference, and xmlns:vm="clr-namespace:Company.Proj.ViewModel;assembly=Company.Proj" - is marked as fine code(NO errors). But I could not see anything in usual dropdovn when I am typing – curiousity May 21 '15 at 15:14
  • Possibly .NET framework versions don't match. Check if your new proj and Company.Proj have same .net framework versions. – Marshal May 21 '15 at 15:44
  • Yes)) your right - when I have corrected framework version I COULD create ViewModel class instance in .cs file (!And it compiles fine!) but COULDn't do it in xaml - still no suggestions in drop down. -error vm:SampleVM was not found Verify that you are not missing an assembly reference and that all referenced assemblies have been build. – curiousity May 21 '15 at 16:00
  • Did you look at the edit link I gave in the above answer. It has may solutions and descriptions given which are helpful. – Marshal May 21 '15 at 16:08
  • yes, I have tried most of them yesterday. The problem is really very common. – curiousity May 21 '15 at 16:17