wpf XAML 中的命名空间错误中不存在名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14665713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name does not exist in the namespace error in XAML
提问by Jeff Davis
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.
使用 VS2012 处理 VB.NET WPF 应用程序。我有一个简单的 MusicPlayer 教程应用程序,用于学习 WPF。我正在逐步将教程的 C# 版本转换为 VB.NET。
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.
它在应用程序中有 2 个类,它们都在同一个命名空间下。我可以在 XAML 中引用命名空间,但是当我尝试在 XAML 中引用类对象时,出现错误并且无法编译。
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.
奇怪的是,IntelliSense 在通过 xmlns:c= 标记引用命名空间以及在键入类对象时使用 <c:
但该对象带有下划线并且在尝试构建或在设计器中工作时生成错误。
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...
.vb 类文件位于名为 \Controls 的文件夹中。主项目根命名空间有意留空。这个类是这样编码的......
Namespace MusicPlayer.Controls
Public Class UpdatingMediaElement
.... code here
End Public
End Namespace
The xaml looks like this
xaml 看起来像这样
(namespace defined in the <Window >
tag
(在<Window >
标签中定义的命名空间
xmlns:c="clr-namespace:MusicPlayer.Controls"
(object defined in a <Grid>
)
(在 a 中定义的对象<Grid>
)
<c:UpdatingMediaElement Name="MyMediaElement" />
(error displayed) The name "UpdatingMediaElement" does not exist in the namespace "clr-namespace:MusicPlayer.Controls".
(显示错误)名称“UpdatingMediaElement”在命名空间“clr-namespace:MusicPlayer.Controls”中不存在。
Not sure what is wrong or how to fix it?
不知道出了什么问题或如何解决?
回答by Toan NC
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).
当您编写 wpf 代码时,VS 告诉“名称 ABCDE 在名称空间 clr-namespace:ABC 中不存在”。但是你完全可以成功构建你的项目,只有一个小小的不便,因为你看不到 UI 设计(或者只是想清理代码)。
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.
在 VS 中,右键单击您的解决方案 -> 属性 -> 配置属性
将打开一个新对话框,尝试将项目配置从 Debug 更改为 Release,反之亦然。
After that, re-build your solution. It can solve your problem.
之后,重新构建您的解决方案。它可以解决您的问题。
回答by Vasanth Sriram
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"
回答by Jasper H Bojsen
I've seen this issue go away by clearing the Xaml Design Shadow Cache. I had the issue with Visual Studio 2015 Update 1.
通过清除 Xaml 设计阴影缓存,我已经看到这个问题消失了。我遇到了 Visual Studio 2015 Update 1 的问题。
In Visual Studio 2015 the Cache is located here:
在 Visual Studio 2015 中,缓存位于此处:
%localappdata%\Microsoft\VisualStudio.0\Designer\ShadowCache
Process:
过程:
- Right-Click on the solution in the Solution Explorer and Choose "Clean Solution"
- Shutdown Visual Studio
- Delete the ShadowCache folder
- Reopened the Visual Studio project
- Rebuild the solution
- 右键单击解决方案资源管理器中的解决方案,然后选择“清洁解决方案”
- 关闭 Visual Studio
- 删除 ShadowCache 文件夹
- 重新打开 Visual Studio 项目
- 重建解决方案
And voila no more namespace errors.
瞧,没有更多的命名空间错误。
回答by Iman
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 errorsat first.
所以不要直接关注这个错误,先关注其他错误。
回答by teynon
Try changing the build target platform to x86 and building the project.
尝试将构建目标平台更改为 x86 并构建项目。
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.
我通过 Subversion 注意到我显然将项目构建平台目标更改为 x64。这是我所做的唯一改变。进行更改后,代码运行了一小段时间,然后开始显示您遇到的相同错误。我将平台目标更改为 x86 进行测试,突然我的设计师又开始工作了。随后,我将其改回x64,问题完全消失了。我怀疑设计者在 x32 中构建了某种缓存代码,当您更改代码时,更改 x64 构建平台会破坏它。
回答by Supa Stix
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.
我是 WPF 的新手,仍然是 VB.net 的新手 - 所以我假设这个错误是由我愚蠢地登顶造成的......假设我是真的!通过将我的项目从共享驱动器移动到我的本地驱动器之一,我设法摆脱了它。错误消失了,项目完全编译没有进一步的问题 - 还没有。看起来 VS2015 在共享驱动器上保存的项目仍然存在问题。
回答by Simon
Maybe another solution for when the project compiles but the XAML error is showing :
当项目编译但显示 XAML 错误时,也许是另一种解决方案:
- In solution explore, on the project node that contains the xaml
- Right-click on the project and choose 'Unload Project'
- Right-click on the project and choose 'Reload Project' Make sure that your project is still choosen as "startup project". If not :
- Right-click on the project and choose 'Set as startup project'
- 在解决方案探索中,在包含 xaml 的项目节点上
- 右键单击项目并选择“卸载项目”
- 右键单击项目并选择“重新加载项目” 确保您的项目仍被选为“启动项目”。如果不 :
- 右键单击项目并选择“设置为启动项目”
No need to rebuild, or close visual studio.
无需重建或关闭 Visual Studio。
回答by Jonas
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.
天啊......这仍然是 Visual Studio 2017 五年后的问题。由于我是 WPF 的新手,我确定问题出在我的身上,但不,一切都正确编译并运行。
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:
我尝试重建、清理和重建、在 x86/x64 输出之间切换、重新启动 Windows、清理 ShadowCache 文件夹、在 XML 命名空间声明中添加“;assembly={my main assembly name}”,但没有任何效果!做的一件事:
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.
将我的静态命令类(在我的情况下,交易是关于让设计发现我的 WPF 命令)在其单独的程序集中,并将程序集名称更改为那个。
回答by gbdavid
I had this problem recently using VS 2015 Update 3 for my WPF project in .NET 4.6.2. The copy of my project was in a network folder, I moved it locally and that solved the problem.
我最近在 .NET 4.6.2 中为我的 WPF 项目使用 VS 2015 Update 3 时遇到了这个问题。我的项目副本位于网络文件夹中,我将其移动到本地并解决了问题。
This may solve other sort of problems, as it looks like VS 2015 doesn't like network paths. Another issue that is a big problem for them is syncing git repositories if my project is in a network path, also solved by moving it locally.
这可能会解决其他类型的问题,因为看起来 VS 2015 不喜欢网络路径。如果我的项目在网络路径中,另一个对他们来说是个大问题的问题是同步 git 存储库,也可以通过将其移动到本地来解决。
回答by Trevy Burgess
The same problem plagues Visual Studios 2013, Service Pack 4. I also tried it with Visual Studios 2015 Preview with the same results.
同样的问题困扰着 Visual Studios 2013, Service Pack 4。我也用 Visual Studios 2015 Preview 尝试过,结果相同。
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.
这只是 Visual Studios 团队尚未修复的 WPF 可视化工具的一个限制。作为证明,在 x86 模式下构建会启用可视化工具,而在 x64 模式下构建会禁用它。
Strangely enough intellisense works for Visual Studios 2013, Service Pack 4.
奇怪的是,智能感知适用于 Visual Studios 2013 Service Pack 4。