Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name does not exist in the namespace error in XAML

Using VS2012 working on a VB.NET WPF application. I have a simple MusicPlayer tutorial app I am using to learn WPF. I am converting a C# version of the tutorial to VB.NET step by step.

It has 2 classes in the app that are both under the same namespace. I am able to reference the namespace in the XAML but when I try to reference the class object in XAML I get an error and I am not able to compile.

Strange thing is that the IntelliSense works fine with both referencing the namespace via the xmlns:c= tag and also when typing the class object using <c: But the object is underlined and errors are generated trying to build or work in the designer.

The .vb class files are in a folder called \Controls. The Main project Root Namespace is intentionaly left blank. The class is coded like this...

Namespace MusicPlayer.Controls
    Public Class UpdatingMediaElement
       .... code here
    End Public
End Namespace

The xaml looks like this

(namespace defined in the <Window > tag

xmlns:c="clr-namespace:MusicPlayer.Controls"

(object defined in a <Grid> )

  <c:UpdatingMediaElement Name="MyMediaElement" />

(error displayed) The name "UpdatingMediaElement" does not exist in the namespace "clr-namespace:MusicPlayer.Controls".

Not sure what is wrong or how to fix it?

like image 517
Jeff Davis Avatar asked Feb 02 '13 19:02

Jeff Davis


8 Answers

If the assembly is different from the namespace in which your class is contained, you have to specfiy it explicitly.

ex:-

xmlns:Local="clr-namespace:MusicPlayer.Controls;assembly=MusicPlayer"
like image 43
Vasanth Sriram Avatar answered Sep 24 '22 00:09

Vasanth Sriram


When you are writing your wpf code and VS tell that "The name ABCDE does not exist in the namespace clr-namespace:ABC". But you can totally build your project successfully, there is only a small inconvenience because you can not see the UI designing (or just want to clean the code).

Try to do these:

  • In VS, right click on your Solution -> Properties -> Configuration Properties

  • A new dialog is opened, try to change the project configurations from Debug to Release or vice versa.

After that, re-build your solution. It can solve your problem.

like image 111
Toan NC Avatar answered Sep 22 '22 00:09

Toan NC


In my case it was because of other compile errors. When other errors have been solved this seemingly related error was also removed from the list. Specially the errors at the bottom of the errors list and on pages you have recently changed.

So do not pay attention to this error directly and focus on other errors at first.

like image 35
Iman Avatar answered Sep 20 '22 00:09

Iman


Maybe another solution for when the project compiles but the XAML error is showing :

  1. In solution explore, on the project node that contains the xaml
  2. Right-click on the project and choose 'Unload Project'
  3. Right-click on the project and choose 'Reload Project' Make sure that your project is still choosen as "startup project". If not :
  4. Right-click on the project and choose 'Set as startup project'

No need to rebuild, or close visual studio.

like image 24
Simon Avatar answered Sep 21 '22 00:09

Simon


Jesus... This is still a problem five years later in Visual Studio 2017. Since I'm new to WPF, I was sure the problem was somehow me, but no, everything compiled and ran correctly.

I tried rebuilding, cleaning and rebuilding, switching between x86/x64 output, rebooting Windows, cleaning the ShadowCache folder, adding ";assembly={my main assembly name}" to the XML namespace declaration, nothing worked! The single thing that did:

Put my static class of Commands (in my case the deal was about making the design discover my WPF Commands) in its separate assembly and changing the assembly name to that one's instead.

like image 37
Jonas Avatar answered Sep 22 '22 00:09

Jonas


I've seen this issue go away by clearing the Xaml Design Shadow Cache. I had the issue with Visual Studio 2015 Update 1.

In Visual Studio 2015 the Cache is located here:

%localappdata%\Microsoft\VisualStudio\14.0\Designer\ShadowCache

Process:

  1. Right-Click on the solution in the Solution Explorer and Choose "Clean Solution"
  2. Shutdown Visual Studio
  3. Delete the ShadowCache folder
  4. Reopened the Visual Studio project
  5. Rebuild the solution

And voila no more namespace errors.

like image 35
1iveowl Avatar answered Sep 23 '22 00:09

1iveowl


Try changing the build target platform to x86 and building the project.

I noticed via Subversion that I apparently changed the project build Platform target to x64. This was the only change I had made. After making that change, the code was working for a short while before it started showing the same error you experienced. I changed the platform target to x86 to test and suddenly my designer was working again. Subsequently, I changed it back to x64, and the problem has disappeared completely. I suspect that the designer builds some kind of cached code in x32 and changing the x64 build platform breaks it when you make code changes.

like image 37
teynon Avatar answered Sep 22 '22 00:09

teynon


Dunno if this will help anyone else

I'm new to WPF and still a novice with VB.net - so I was assuming that getting this error was being caused by me doing summit silly........ suppose I was really! I've managed to get rid of it by moving my project from a shared drive to one of my local drives. Error's disappeared, project compiles perfectly no further issues - yet. Looks like VS2015 still has problems with projects held on a shared drive.

like image 27
Supa Stix Avatar answered Sep 20 '22 00:09

Supa Stix