35

Now that's a real strange error. I am working on a WPF application and following MVVM. In my MainWindow I am setting views and view models and I get this strange error. Although it builds fine and application runs fine, but why I am getting this error.

I also followed some similar but didn't find appropriate answer. I tried to restart visual studio and clean and rebuild, but still I face this error.

So here is the code.

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:MyProject.Views"
        xmlns:vm="clr-namespace:MyProject.ViewModels"
        xmlns:p="clr-namespace:MyProject.Properties"
        Title="{x:Static p:Resources.Title}" Height="400" Width="750" MinHeight="400" MinWidth="750">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:MainPageViewModel}">
            <v:MainPageView/>
        </DataTemplate>
    </Window.Resources>

Error   1   The name "MainPageViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels".

Here is my ViewModel

namespace MyProject.ViewModels
{
    public class MainPageViewModel : PropertyChangedBase
    {
        public MainPageViewModel()
        {
        }
    }
}

So what is real error. I am using Visual Studio 2012 by the way.

Update: My viewmodels and views are in the same project. I am not referencing to any other project. And MyProject.ViewModels.MainPageViewModel exists.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112

20 Answers20

36

clr-namespace:MyProject.ViewModels

There has to be a namespace called MyProject.ViewModels that has a class Called MainPageViewModel that is public and has a public parameterless constructor within the same assembly as ProjectDatabaseRebuilder.MainWindow.

There is not.

If MyProject.ViewModels exists in a referenced assembly, you must state so within the xmlns.

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

or some such. Honestly, it looks like you copypasted someone's example without realizing how these specialized xml namespaces work in WPF.

Something tells me the ultimate answer will be the following: xmlns:vm="clr-namespace:ProjectDatabaseRebuilder.ViewModels".

Please note that "namespace" and (as above) "assembly" mean namespaces and assemblies, and the xaml deserializer uses this information to locate types at runtime. If they are incorrect, things won't work.


This is trivial. You must have done something weird with your project, which might necessitate you start from scratch. Or, you can make a new project following my guide below, and then compare it bit by bit to yours to see where you went wrong.

First, create a new WPF application called MyWpfApplication.

Add a folder for Views and one for ViewModels. Add the shown VM code class and view UserControl:

enter image description here

In your code class, add the following:

namespace MyWpfApplication.ViewModels
{
    class MainWindowViewModel
    {
        public string Text { get; set; }

        public MainWindowViewModel()
        {
            Text = "It works.";
        }
    }
}

Your View is also simple:

<UserControl
    x:Class="MyWpfApplication.Views.MainWindowView"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid>
        <TextBlock
            Text="{Binding Text}"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />
    </Grid>
</UserControl>

And, in your Window, do essentially what you are attempting to do:

<Window
    x:Class="MyWpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v="clr-namespace:MyWpfApplication.Views"
    xmlns:vm="clr-namespace:MyWpfApplication.ViewModels"
    Title="DERP"
    Content="{Binding DataContext, RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate
            DataType="{x:Type vm:MainWindowViewModel}">
            <v:MainWindowView />
        </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <vm:MainWindowViewModel />
    </Window.DataContext>
</Window>

And, when run, you should see everything as expected:

enter image description here

Do this, then compare the working solution to your project. If you can't find any differences, you probably need to just scrap your solution and start fresh. Copy code, not files, into the new solution.

  • Sorry, actually I renamed my original project name with MyProject. I edited that in the question as well. – fhnaseer Jun 10 '13 at 18:08
  • @FaisalHafeez: What you're doing is correct. How you're doing it may not be. Clean, rebuild, re-run. Make sure your VM class is public, as well. –  Jun 10 '13 at 18:10
  • I tried clean rebuild and re run. Error is still there. VM is public. If I start the application it runs fine, my view is displayed and it is bind to the VM. But in error window it still shows the error. May be its Visual Studio error, don't know. – fhnaseer Jun 10 '13 at 20:43
  • I found this on stackoverflow. This says that "Your solution is running on a network" what does that mean. I am not sure. share.http://stackoverflow.com/questions/16216662/the-name-xyz-does-not-exist-in-the-namespace-clr-namespaceabc?rq=1 – fhnaseer Jun 10 '13 at 20:45
  • I am using TFS. And I have mapped projects on my local machine. So they exist on location D:\SomePathLeadingToMyProject. Do I need to do something else? – fhnaseer Jun 11 '13 at 05:01
  • @FaisalHafeez: Restart VS. Remove files from the solution. Re-add them. This is some weirdness. –  Jun 11 '13 at 14:05
  • @FaisalHafeez: I pulled code from a prototype and added it to my answer. –  Jun 11 '13 at 14:27
  • I'm using someone else's code and getting this error. Could it be because he put the viewmodels and views at the root level of the WPF project? – Rod Apr 17 '17 at 21:16
  • @rod no. Make sure they are public, make sure they are referenced via project and not by browsing to the bin folder. –  Apr 18 '17 at 12:27
24

Visual Studio expects namespaces to match folder locations.
To solve your problem, exit Visual Studio and rename your project folder to MyProject. Then start Visual Studio, remove the project from the solution, add it again as "existing project" and build the project, F6 or ctrl + shift + B

If you rename your namespace after the project has been created you will get these kinds of bugs.

mtfurlan
  • 1,024
  • 2
  • 14
  • 25
brandon
  • 555
  • 1
  • 6
  • 15
  • 1
    Thank you for this. I was scratching my head because of this error for days and I was sure I haven't done anything wrong. In my case, because of an error with my measurement studio licence, I had to start my projects from NI example and had to rename the project. I had this dreadful error which means my project was building but design view shows this error and is not available which makes changing the code difficult. Your suggestion solved the issue immediately. Thank you. – falopsy Jan 02 '14 at 12:05
  • Oh man, thanks a lot! You solved a problem I've been fighting for years. Unfortunatelly I can upvote only once. – Jan Zahradník May 29 '15 at 21:51
  • Haha yeah, copied my project from an existing one and renamed everything but the old folder name itself. Did as you suggested and now I can see my views namespace views without fault. I +1'd your response. – Robbie Smith Jun 26 '17 at 04:37
18

I had the same problem. I had the right namespace, the right class name, and I even updated the PATH. Finally, I took out the "DataType" from the XAML, got it to compile, the added the DataType back in and it worked. Seems like it was a chicken and egg problem. The XAML doesn't see it until it has been compiled in, and you can't compile with it in the DataType. So, create the MV first, and compile it. Then, add to XAML.

Dan Hewett
  • 2,200
  • 1
  • 14
  • 18
12

Rebuild your solution (sometimes clean then build works better). Then look at your error list, scroll to the very bottom, and it will most likely indicate an error that is not allowing your assembly to compile, and the XAML compiler is most likely using a cached version of the assembly, not the new one you mean to build.

John C
  • 880
  • 10
  • 13
  • Thanks! This really helped me. I was going out of my mind, because VS automatically imported the namesapce, so clearly the paths were right and the view was there. Re building clear the issue right away. – Curtwagner1984 Jul 06 '22 at 06:14
7

Close and reopen Visual Studio.

I tried some of the answers here (I'm using VS2012 with Resharper), and I was doing everything ok, but still had the error. I was even able to navigate to my bound fields using Resharper's 'cntl'+click, but was still getting XAML compile errors and designer wouldn't show the design view. Closing visual studio and reopening fixed it for me.

Gordon Slysz
  • 1,083
  • 12
  • 23
  • Exactly !!!! Its not the first time... I should not tell you how many times I felt in this behavior... I'm ashamed. Thanks a lots! – Eric Ouellet Mar 10 '16 at 21:18
6

This error occurs because a xaml file references to unbuilt namespace.

My Solution [ in 3 steps ]:

1- Comment a problematic file[s] and replace with empty version. for example for yours:

<!-- <Window ...> </Window> -->  <!--you orginal xaml code!, uncomment later-->

<Window                          <!-- temp xaml code, remove later -->
    x:Class="MyProject.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>

2- Clean and Rebuild the Project/Solution.

3-Uncomment original xaml code and remove temp xaml code.

it's done.

Ayub
  • 2,345
  • 27
  • 29
5

Check the Configuration Manager in Visual Studio. Make sure all projects match platform. I wanted platform to be x64, but my primary application was set to AnyCPU. Set that to x64 and it took care of the problem.

Greg Osborne
  • 49
  • 1
  • 2
4

I had the same problem. I'd used Intellisense to build the namespace mapping and it had not included the assembly attribute, so it looked like this:

xmlns:converters="clr-namespace:XYZ.UI.Converters;

when I compared it to a working Behavior in another window, I spotted the difference. When I changed it to

xmlns:converters="clr-namespace:XYZ.UI.Converters;assembly=XYZ.UI"

cleaned it and built it, it worked.

user2871239
  • 1,499
  • 2
  • 11
  • 27
4

For us it was simply that the Dll Project was set to Platform Target x64 in Visual Studio 2013. After setting it to Any. Everything starts working again.

lanwin
  • 540
  • 4
  • 6
  • 1
    Thanks for that; helped me as well when nothing else did. I had forced x64 since the project refers to unmanaged x64-only code, but at least it still builds for x64. So switching to "Any CPU" temporarily, makes that strange error go away and the Designer is working again... – t_3 Sep 08 '16 at 06:33
2

I have been through this with Microsoft. The answer, in summary, is to add your build output folder to your PATH.

Apparently, the designer in Visual Studio loads DLLs and their dependencies. When it can't find a dependency, it fails silently and you get these types of errors.

Adding the build output folder to my PATH is not really an option in my case, because I build several different configurations and they all go to separate folders. The last thing I need to encounter some weird problem caused by a DLL being picked up from a location I hadn't anticipated.

Edit: Sorry. In my haste, I should have pointed out that the folder(s) to add to the PATH are the folder(s) containing the dependent DLLs. In my case, this is the build output folder, but that may not be true in all cases.

JAB
  • 313
  • 2
  • 10
  • This solution is also useful for a problem with the XAML designer when dealing with C++/CLI DLLs as described in [this](http://stackoverflow.com/q/6036631/2439414) and [this](http://stackoverflow.com/q/278416/2439414) question. – xandriksson Jun 23 '16 at 14:52
2

None of the other answers solved the problem for me, but I eventually came up with an answer that did. In my case, this error only caused the designer to complain about invalid markup (i.e. everything worked fine at runtime).

I had used ReSharper8 to rename one of my classes being referenced in the XAML. This caused another completely different class reference in that same XAML to have this error. This was confusing because the class causing the problem was NOT the same as the referenced class displaying the error.

One of the other answers indicated that the error in question could be caused by renaming a project, and the only way to fix it was to remove and re-add that project. Similarly, I deleted and re-created the class file that I had renamed and the designer begin working again.

bugged87
  • 3,026
  • 2
  • 26
  • 42
1

I ran into this error, and the problem was that I had a class within the project with the same name of the assembly I was trying to reference. I changed the class name and it worked.

bsguedes
  • 731
  • 6
  • 15
1

I ran into this and had trouble working through it based on the answers posted in this forum. I had renamed my project at some point and had introduced a typo into the auto-generated AssemblyInfo.cs file (in the assembly: AssemblyTitle and assembly: AssemblyProduct fields). Surprisingly, this was the only compilation error that I got as a result. Just something a bit non-obvious to double-check if you're getting this error and the previous suggestions don't work.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
1

The solution is easy, just comment your DataType code and compile it first, uncomment the code back, then the problem is solved.

XAML file just cannot refererence to namespaces that haven't been built.

Hunter Hsieh
  • 11
  • 1
  • 1
1

This one's been tying me in knots for hours.

I have a VS2015 solution which is several years old. On my laptop, it builds beautifully, all the code is checked into TFS, but when I tried to build it on a different machine, I'd constantly get this message about the namespace not existing.

Bad Namespace

Basically, this RepBaseWindow is a class inherited from the WPF Window class, with a few extra bells'n'whistles.

public class RepBaseWindow : Window

I'd tried a load of the suggestions on here, but no luck.

The irony is that, despite this message, if I right-clicked on this faulty link and selected "View Code", it'd happily take me to this class. The namespace was fine, the class was fine... but WPF wasn't happy about it.

If I tried to use Intellisense to find the namespace, I'd end up with the same markup... and the same Build Errors.

I compared the working copy of the code with the TFS version, and realised the problem.

I needed to go back to using an old copy of the WindowBase.dll file from 2012.

Ridiculous, hey ?

The working version of WindowBase.dll is 635kb in size, and shows this in the details tab:

enter image description here

I'm just posting this answer to show that, sometimes, these errors have absolutely nothing to do with faulty WPF markup or wrongly-spelt namespace names.

Hope this helps.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

In my case implementation of IValueConverter was the problem

I have changed:

public object Convert(object value, 
  Type targetType, 
  object parameter, 
  string language) 
{ //code }

to

public object Convert(object value, 
  Type targetType, 
  object parameter, 
  System.Globalization.CultureInfo culture) 
{ //code } 

...rebuild project

Dariusz Filipiak
  • 2,858
  • 5
  • 28
  • 39
0

I think how I ran into this was to create the View first, and pointed to a ViewModel that didnt exist. Afterwards I create the ViewModel. Kept getting error, could not find ViewModel.

Finally I noticed that the path of newly created View page was pointing to bin directory instead of Views directory. So, I left ViewModel alone. Deleted and recreated View again. Right Clicked and Cleaned the Project, closed all files opened within Solution, and Rebuilt. That finally fixed it.

ekgcorp
  • 165
  • 1
  • 11
0

I had the same problem and when I added a DEFAULT CONSTRUCTOR to my ViewModel and built my software (ctrl+Shift+B), it started realizing the ViewModel in the specified namespace!

Ehsan
  • 767
  • 7
  • 18
0

It happens pretty often that it can not find the class or whatever you are referring to. Often the best solution is to comment out the reference in XAML and rebuild the solution and try again - always works for me!

Steffen
  • 140
  • 1
  • 5
0

I had the same problem, in my case the following simple solution helped me to solve this problem:

1- Clean Solution

2- Rebuild Solution

Then this error will disappear.

zalky
  • 659
  • 1
  • 7
  • 12