101

I have a small WPF application which used to compile just fine but is not anymore. I can't really say at which point it stopped building. It just worked fine one day, and the next it's not.

Here's the project structure:

enter image description here

There is no other projects or external references other than standard .net dlls.

Here's the user control where the problem originated:

<UserControl x:Class="TimeRecorder.HistoryUserControl"
         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" 
         xmlns:local="clr-namespace:TimeRecorder.ViewModel"
         xmlns:framework="clr-namespace:TimeRecorder.Framework"
         mc:Ignorable="d" Height="Auto" Width="Auto" Padding="5">
<UserControl.Resources>
    <local:HistoryViewModel x:Key="ViewModel"/>
    <framework:BoolToColorConverter x:Key="ColorConverter"/>
</UserControl.Resources>
<StackPanel DataContext="{StaticResource ViewModel}">

And here's the error I get: http://i48.tinypic.com/5u1u8w.png

Please note that this is not just the one file in the screenshot, but all references I add in similar fashion in xaml in all user control/window files in this project.

So the file is there, the namespace in the file is correct, the namespace/class name in the xaml file is (to my understanding) correct. I get intellisense when I type in the xaml so it finds the files ok then but not when it compiles.

The most common solution for this in other posts has been the .net framework version. It is currently set to .Net Framework 4 for both my main and test project. The full version not the client profile.

Here's what I think I messed up: In the configuration manager, both projects have their Platform set to Any CPU, but at one point when trying to solve this I noticed that the main project was set to x86 and the test project was set to Any CPU. So I added Any CPU manually for the main project in the configuration manager. However I honestly don't know if I did this correctly or even if I should do it. So as an additional question, is there a way I can reset the configuration manager to its default state? Will this have anything to say for the main problem? I don't know if the main project was always set to x86 or not or if I somehow changed it to x86 and then it broke. As mentioned this project was compiling just fine for a while.

halfer
  • 19,824
  • 17
  • 99
  • 186
ardal
  • 1,537
  • 3
  • 14
  • 18
  • FYI, you can read ProgressTimeSpentUserControl.xaml. You might want to do a better job blurring it ;) – It'sNotALie. Feb 24 '13 at 11:13
  • No biggie :) It's just a temp file I was testing some stuff in, where as the others are part of the project with belonging view models so my OCD told me to mark it as not relevant ;) – ardal Feb 24 '13 at 11:48
  • 2
    I had a very similar problem 2 days ago - I messed around with the configuration manager trying to set everything to build as 'any cpu' rather than x86 and it stopped building. I found that I had done two things - firstly I had left it in release mode, and the secondly the build configuration manager the assembly was in some cases not marked for building. My fix was to go thorugh every mode available (x86, mixed platforms and any cpu) and set all the assemblies to build. It sounds like one of those things that you will kick yourself for not thinking of when you do discover what is wrong! – Jay Feb 24 '13 at 18:53
  • Similar question (with working answer): http://stackoverflow.com/questions/28216096/wpf-the-name-does-not-exist-in-the-namespace – Oleksa Aug 09 '16 at 13:22
  • For anyone landing here in 2018 .NET 4.6.1 I restarted VS, then rebuilt, and it started working. Definitely spent more time than I should have thinking I had a type-o somewhere. – Mwspencer Mar 13 '18 at 16:08
  • Does this answer your question? [The name does not exist in the namespace error in XAML](https://stackoverflow.com/questions/14665713/the-name-does-not-exist-in-the-namespace-error-in-xaml) – StayOnTarget Apr 07 '20 at 19:01

32 Answers32

179

Every time it happend to me i just restarted visual studio, re-built the solution and it worked just fine.. can't say why

gil kr
  • 2,190
  • 1
  • 17
  • 23
  • 19
    One thing to add - only when I restart VS with admin rights the project build up successfully. With regular permissions even re-starting and re-building solution did non help. – Sevenate Jun 09 '13 at 20:57
  • 1
    Just save the solution that is causing the problem. Start VS in admin mode and start "rebuild all" the from the solution file. it worked. very spooky. – pollaris Mar 02 '17 at 21:04
  • @gil kr I had similar issues with xamarin and its XML. I found moving the project from a networked folder to a local folder fixed the issue. I wonder if that is the case here. – vdidxho Mar 01 '18 at 22:37
  • 4
    It did not solve my problem, even starting with Administer rights. – MindRoasterMir Jan 07 '19 at 15:31
  • The solution that worked for me was to delete the VS hidden cache directory ".vs". I can't say this is ideal for many because it does delete some user data like the current opened files. It did build right after I did that without any the confusing error. – Sn3akyP3t3 May 05 '19 at 18:45
  • Just looking at the same issue. With VS 2019 I just had to rebuild the solution and it found the classes that were causing the error. I didn't need to restart VS. – Rob Oct 29 '19 at 11:51
  • I have already commented a year ago. It did not work but what worked was to remove the code complaining. Run the project and it runs successfully. Now add the code again, and you will get no errors anymore. This solution is also given as answer ( scroll down). Thanks – MindRoasterMir Aug 02 '20 at 09:24
  • I solved it by `building` my app first then add my (user control) – Ebrahim ElSayed Aug 10 '20 at 21:00
  • I had to just reload (unload project then load project) and build – weshouman Jun 15 '21 at 08:29
32

In addition to the "does not exist in the namespace" message, I was also getting a message from the designer that it could not display the window for x64 and ARM targets.

I have just found that switching the build to x86 mode, doing a rebuild solution, then switching back to x64 mode and then rebuilding again fixes [both] problems.

Simply rebuilding the x64 solution did nothing.

Jerry
  • 393
  • 4
  • 8
  • 5
    There's one more step. You have to rebuild to x86 and then open the xaml in the designer. And only then go back to x64. – Dzienny Jan 06 '14 at 14:25
  • 2
    I tried restarting visual studio then rebuilding but was still getting the same error. @Jerry - your solution was the one that worked for me in VS2012. – Barrie Jul 29 '14 at 15:52
  • Jerry, it sounds dubious but your solution works! I compiled my project for AnyCPU but somehow compiled under x86 then back solved this problem. I suspect AnyCPU may fall back to x86 but because of a bug in VS, that part of code was not built unless compile it under x86. – Wayne Lo Dec 30 '15 at 22:32
  • Yes, any OTHER errors in addition to the "does not exist in the namespace" must be resolved first. – pollaris Mar 03 '17 at 14:25
  • Still happened to me in VS2017. Closing and reopening VS didn't work. But your solution did. – Gordon Slysz Jun 16 '17 at 15:35
  • Similar solution worked for me. I've been working with AnyCPU but the designer preview wouldn't load until I built both x86 **and** x64 targets. – evanjs Jul 07 '17 at 00:45
  • I too can't fathom why it worked, but it did. One additional thing I encountered. After switching to x86, rebuilding, and switching back, I got a build error in my XAML because I was using "Name=" on my custom Window element and it wanted me to use "x:Name=". Once I did that the problems went away – Mageician Nov 03 '18 at 03:11
  • Don't forget to restart Visual Studio AGAIN after rebuilding--at least that's what I had to do just now. – Aufgeschissener Kunde Aug 07 '23 at 20:36
17

What I found that helped (especially if this error occurs in App.xaml) is to comment out the reference(s) that gives you trouble, rebuild, then uncomment. I think what this does is allows the entire project to actually build instead of stopping the build at the error.

From what I can gather, the app is trying to build the files in a certain order, so when App.xaml or presumably any other class file errors in a reference, the file that is causing the error hasn't been compiled correctly, hence why it doesn't find the file in that namespace.

jaysoncopes
  • 805
  • 3
  • 13
  • 26
  • 4
    You had the right idea when you said: `the app is trying to build the files in a certain order`. This made me look into the my `.csproj` file. There `App.xaml.cs` was at first place. I then moved it under the file that showed this error, rebuild, and the error is gone. THANKS! – Stephan May 06 '16 at 12:45
12

This is what worked for me on Visual Studio 2012 (Update 3).

  • Restart Visual Studio
  • Add current assembly to namespace declaration xmlns:framework="clr-namespace:TimeRecorder.Framework;assembly=MyAssembly
  • Build -> Build Solution
Leon Storey
  • 3,274
  • 2
  • 25
  • 40
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
  • This, and after fixing all the C# bugs the build was successful, but the XAML errors would remain in the designer. Then you have to restart VS to be clean, again. Can't believe this madness. After all these years this is still a relevant problem. Microsoft ... – Martin Braun Nov 04 '21 at 02:41
8

What worked for me: - Switch solution configuration from Debug to Release - Switch back configuration from Release to Debug

Hugues
  • 81
  • 1
  • 1
8

I had the similar issue. In my case, I had to do the following

  • remove the referencing markup from xaml (in this example, <local:HistoryViewModel x:Key="ViewModel"/>)
  • build the Class ( in this example file which contains HistoryViewModel class )
  • Once its built, add the referencing markup in xaml
  • build again

The above method worked for me.

chank
  • 3,546
  • 1
  • 14
  • 22
6

Ran into this issue today with Visual Studio 2017 Community Edition. Tried all the suggestions here (reset VS 2017, changed from x64 to x32 and back again etc etc) and from other sources to no avail. Intellisense knows everything is there but I was getting the same error everytime.

Anyway, my fix turned out to be very simple ... aren't they always when you have spent a couple of hours on the problem!

Basically, I did the following ...

  1. Remove offending code from xaml file (just 3 lines in my case)
  2. Build project so you get a successful build
  3. At this point the layout magically appeared in the designer window which was a good sign!
  4. Reinserted the code I removed in point 1. including the xmlns: entry
  5. At this point you shouldn't get any blue squiggles ... hopefully
  6. Build the project again

It's seem that by getting a successful build, it must reset 'something' within VS and/or the assembly. Once you have a successful build try inserting your code again.

halfer
  • 19,824
  • 17
  • 99
  • 186
DynamicL
  • 61
  • 1
  • 2
4

Had this problem going round in circles wasting a few hours. I moved a separate user control dll into the project so it was compiled in the project and not a dll referenced. This broke the whole project so I then went through checking meticulously all namespaces, paths and file names. Tried deleting obj files, changing between release and debug, between x86 and AnyCPU. Opening saving all, recompile still no joy.

Remember having a similar problem before previously, the error flagged in VS2013 was not directly related to where I had to modify the XAML but by using

x:Name="myControl"

on all controls, instead of

Name="myControl"

fixed it.

Moon Waxing
  • 695
  • 13
  • 19
  • Just caught out with the same problem again leaving off the x: Note this only happens with my own user control. – Moon Waxing Oct 24 '15 at 02:52
3

None of the solutions worked for me. I fixed it this way:

  • Remove the dll of the library from the References
  • Download the source code of the library (instead of just the dll file)
  • Build the library's project to get a new dll file
  • Add the new dll file to the References of the main project
Noxxys
  • 879
  • 1
  • 9
  • 19
3

I Changed Target Framework My Application of ".Net Framework 4.5" to ".Net Framework 4.6" and it worked!

amir stack
  • 217
  • 1
  • 12
2

Here's a weird example of a similar thing:

<UserControl x:Class="Gtl.Ui.Controls.WaitControl"
         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" 
         xmlns:controls="clr-namespace:Gtl.Ui.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="120" d:DesignWidth="120"             
         Background="Transparent">
...
</UserControl>

will compile (VS2013).

<UserControl x:Class="Gtl.Ui.Controls.WaitControl"
         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" 
         xmlns:controls="clr-namespace:Gtl.Ui.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="120" d:DesignWidth="120"             
         Background="Transparent"
         IsVisibleChanged=onIsVisibleChanged>
...
</UserControl>

produces the error "type Ui not found in Gtl.Ui.Gtl" (and I assure you the handler method exists in the code-behind). The work-around is to add the handler in the class constructor, but c'mon Microsoft, wtf is going on?

Julian Gold
  • 1,246
  • 2
  • 19
  • 40
  • I disagree with the above comments. This sort of "context widening" is often useful for identifying the cause of such a bug, which isn't ONLY caused by the OP's conditions. – CJBrew Apr 19 '16 at 13:05
2

I faced the same issue when i was trying to call the namespace in xaml. was showing that class is not available in the namespace. I searched a lot. Finally i found this issue was with VS. I am using VS 2013. I tried below steps:

  1. Build-->Configuration Manager--> Active Solution Platform --> Changed to x64 and x86 and Any CPU.
  2. Closed the VS and opened again.
  3. Change

    xmlns:VM="clr-namespace:MyFirstAppViewModel"
    

    to

    xmlns:VM="clr-namespace:MyFirstAppViewModel;assembly=ViewModel"
    
smartrahat
  • 5,381
  • 6
  • 47
  • 68
2

This error usually occurs when project was not build successfully during the last build.

Step-1) First remove all the error causing code from the XAML or .cs file and build & start the project by pressing F5.

Step-2) Add add your error causing code in XAML one by one.

Laxman Marothiya
  • 383
  • 3
  • 13
2

A complete exit and restart of Visual Studio does often resolve these issues, as stated by a few people who have answered this post already. However, sometimes the errors will not be resolved because it is actually a build failure or a dependency issue instead.

When I have worked on WPF projects in Visual Studio, the XAML errors are not always the underlying cause, but rather a symptom of what is failing. Sometimes there are .net framework dependency issues those don’t show up in there Error List windows and you have to use the Output window to debug what is actually failing.

I have learned that the underlying cause (of why so many errors light up across multiple XAML files) can actually be an incorrect manifestation when the actual culprit originates from a completely separate class or project. Often resulting in a build failure. Especially in solutions with multiple projects, there can be a DLL dependency that fails to generate by one project, leading to a cascading failure that shows up as XAML document errors.

One of the downsides of XAML is that it requires a built copy of the library in order to verify the XAML since the element names match to actual classes. So sometimes you will get XML errors because a library didn't build correctly. Also when adding new controls, if you haven't built the project it won't be able to find the new classes. Just something to keep an eye on when looking at the errors.

Once you fix the underlying build issues outside of the XAML files, it will allow each of the dependent projects to build. In these situations, the errors being thrown across multiple XAML files have nothing to do with the document structure, but rather that the failure of underlying dependencies; it can break the affected XAML document's ability to render the required properties and bindings. This is misleading initially, but helpful for finding the underlying issue if you understand it.

Michael M
  • 1,303
  • 1
  • 12
  • 12
1
  • i would recommend to Rename x:Key="ViewModel" maybe there is a glitch
  • and if you type local: does VS show you HistoryViewModel?
  • also check if your Class is public
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
1

Just run code analysis from Build menu

Bembo
  • 1,859
  • 1
  • 13
  • 6
1

I found that running the command "Run Code Analysis" re-builds everything and almost always fixes the problem (right click project > Analyze > Run Code Analysis). This also generally re-builds the resource files also so that strings, etc. can be found.

Jeff
  • 646
  • 1
  • 7
  • 13
  • Except, as I've recently found, even this will not work. I had to close VS and restart. Oh, well... – Jeff Aug 06 '18 at 03:42
1

Tried all solutions on this thread but none worked. It turned out to be cause by the solution configuration. My WPF app was set to build for X64 because of some native dependencies that require it but the solution configuration was still set to AnyCPU for the project. Creating a new X64 configuration for the project in the solution configuration manager allowed the XAML designer to finally recognize my type and namespace.

Xeaz
  • 340
  • 4
  • 13
1

Before adding the name space to your .xaml file, please make sure you are not having any compilation errors due to existing code.

Once all the compilation checks are OK, then rebuild your solution and try adding the required namespace to use its class or properties.

Mahesh Anakali
  • 344
  • 1
  • 8
1

This may happen if you have errors other than this specific error also. More errors may cause the file not to compile properly and resulting in this error.

First try to remove other build errors and if possible warnings also. Then rebuild the solution.

0

This is a recurring problem for me. One of the time I found the solution looking into the Warning tab. It was a .NET framework version issue and it stated the following:

Warning 9 The primary reference "myDll" could not be resolved because it was built against the ".NETFramework,Version=v4.5.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".

André Santaló
  • 639
  • 1
  • 9
  • 24
0

There is a glitch with their buffering of the objects layouts. If anything gets renamed or moved, it gets lost. What generally works for me is to create a completely new class and copy in all the old code, get it working on the new class, then remove the original class. Sometimes after you get it up and running with the new class name, you can try renaming it back to the original name (but usually not)

DanW
  • 1,976
  • 18
  • 14
0

I was using xmlns:local="using:MyRootNamespace.ChildNamespace" at the header of the .xaml , and i turned it into xmlns:local="clr-namespace:MyRootNamespace.ChildNamespace" ... well, I just let intellisense do the job, and it worked.

Rick
  • 528
  • 1
  • 6
  • 15
0

The problem is that when you create the x86 target, the output path for the particular project is set to bin\x86\Debug. It looks like Expression blend doesn't like this at all. It seems to only interested in whats in bin\Debug.

If you changed your output path(s) for the x86 project to bin\debug for example, then I'm sure you'd find it will work. Well, works for me anyway :)

RobM
  • 111
  • 3
0

The Target Framework of the .dll file you adding should be the same as the Target Framework of your app.

0

I was facing the same issue. You get this error but still you can build your project successfully, The inconvenience is that you can not see the UI designing (or just want to clean the code & remove annoying wiggly lines). Read many posts are tried several things but following works like a charm.

Tried this in Visual Studio 2019:

Right click on your Solution -> Properties -> Configuration Properties, then change the project configurations from Debug to Release or vice versa.

After that, rebuild your solution. It can solve your problem.

Nitish Kumar Pal
  • 2,738
  • 3
  • 18
  • 23
0

https://i.stack.imgur.com/2oEWs.png

https://i.stack.imgur.com/dMwNX.png

Had the same issue and I resolved by keeping the exe inside the net5.0-windows folder.

Do not try to remove it in Post-build event command line.

YepWPF
  • 1
0

"the name <...> does not exist in the namespace clr-namespace <...>" If this error persist, no matter what you do, there's most likely other error(s) in your solution that is related to the XAML file. For me the error got resolved when I solved other issues in my solution.

0

If the xaml file has the correct namespaces and if you have recently modified the names of any projects within the solution, then here's something to try:

  1. Clean the whole solution
  2. Rebuild each project starting from the lowest in the dependency hierarchy to the highest one by one. Thus, if Project A depends on Project B and Project C, both of which have no further dependencies, then rebuild B and C first, followed by A.
  3. Unload the project with the errors and reload it. Then close and open the sln again if the errors persist.
basecamp
  • 81
  • 1
  • 5
0

By me the issue was that I renamed a view file after creating it so the class name didn't change so it actually wasn't able to import the view, hope this helps someone

big boy
  • 315
  • 2
  • 13
0

To resolve this error, I have commented the section of code showing error in a xaml and build. Then it started showing error in some other file. In that file I found that I have wrongly mentioned the class path <Window xmlns:local="clr-namespace:Test.GUI" instead of <Window xmlns:local="clr-namespace:Test".

After correcting this class path it worked for me.

Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25