The name does not exist in the namespace error in

2019-01-03 13:40发布

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?

27条回答
男人必须洒脱
2楼-- · 2019-01-03 13:55

I had the solution stored on a network share and every time I opened it I would get the warning about untrusted sources. I moved it to a local drive and the "namespace does not exist" error went away as well.

查看更多
爷的心禁止访问
3楼-- · 2019-01-03 13:56

This problem can also be caused if the assembly that you're referencing isn't actually built. For example, if your xaml is in Assembly1 and you're referencing a class also in Assembly1, but that assembly has errors and isn't building, this error will be shown.

I feel silly about it, but in my case I was tearing asunder a user control and had all sorts of errors in the related classes as a result. As I was attempting to fix them all I started with the errors in question, not realising that xaml relies on built assemblies to find these references (unlike c#/vb code which can work it out even before you build).

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-03 13:58

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.

查看更多
Emotional °昔
5楼-- · 2019-01-03 14:01

The same problem plagues Visual Studios 2013, Service Pack 4. I also tried it with Visual Studios 2015 Preview with the same results.

It's just a limitation of the WPF visualizer which the Visual Studios team hasn't fixed. As proof, building in x86 mode enables the visualizer and building in x64 mode disables it.

Strangely enough intellisense works for Visual Studios 2013, Service Pack 4.

查看更多
Melony?
6楼-- · 2019-01-03 14:01

In my case I had a namespace and class spelled exactly the same, so for example, one of my namespaces was

firstDepth.secondDepth.Fubar

which contains its own classes (e.g. firstDepth.secondDepth.Fubar.someclass)

but I also had a 'Fubar' class in the namespace

firstDepth.secondDepth

which textually resolves to the same as the Fubar namespace above.

Don't do this

查看更多
闹够了就滚
7楼-- · 2019-01-03 14:02

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.

查看更多
登录 后发表回答