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:51

Try verifying your assembly references. If you have a yellow exclamation mark on the project references there's a problem there and you'll get all kinds of errors.

If you know the project reference is correct, check the Target framework. For instance, having a project using the 4.5 framework reference a project with 4.5.2 framework is not a good combination.

查看更多
我只想做你的唯一
3楼-- · 2019-01-03 13:52

Add an empty constructor for your view model and rebuild solution. I tried so many solutions not working but this solved for me.

查看更多
贪生不怕死
4楼-- · 2019-01-03 13:53

Adding to the pile.

Mine was the assembly name of the WPF application was the same assembly name as a referenced dll. So make sure you don't have duplicate assembly names in any of your projects.

查看更多
唯我独甜
5楼-- · 2019-01-03 13:53

I'm also having a lot of trouble with this one! Intellisense helps me complete the namespace and everything, but the compiler cries. I've tried everything I found in this and other threads. However in my case what helped in the end was writing something like this: xmlns:util="clr-namespace:LiveSpielTool.Utils;assembly="

Leaving the assembly name empty. No idea why. But it was mentioned here. I must add I am developing an assembly, so the assembly attribute might make sense. But entering the assembly name did not work. So weird.

查看更多
欢心
6楼-- · 2019-01-03 13:54

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.

查看更多
仙女界的扛把子
7楼-- · 2019-01-03 13:54

Also try to right click on your project->properties and change Platform target to Any CPU and rebuild, it will then work. This worked for me

查看更多
登录 后发表回答