33
votes

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.

19
Would be very helpful to see the code for the ViewModel as well, not just the XAML.EtherDragon
@EtherDragon Added ViewModel code to the question.fhnaseer
I've just resolve same problem using instruction from this answer stackoverflow.com/a/12406977/3917754demo

19 Answers

35
votes

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.

23
votes

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.

18
votes

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.

10
votes

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.

7
votes

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.

5
votes

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.

4
votes

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.

4
votes

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.

4
votes

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.

2
votes

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.

2
votes

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.

1
votes

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.

1
votes

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.

1
votes

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.

1
votes

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.

0
votes

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

0
votes

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.

0
votes

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!

0
votes

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!