C# 名称 <...> 在命名空间 clr-namespace <...> 中不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15051121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:53:33  来源:igfitidea点击:

the name <...> does not exist in the namespace clr-namespace <...>

c#.netwpfxamlnamespaces

提问by ardal

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.

我有一个小的 WPF 应用程序,它曾经可以很好地编译,但现在已经不行了。我真的不能说它在什么时候停止建造。它只是在一天工作得很好,然后就不行了。

Here's the project structure:

这是项目结构:

enter image description here

在此处输入图片说明

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

除了标准的 .net dll 之外,没有其他项目或外部引用。

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

这是我得到的错误: 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.

请注意,这不仅仅是屏幕截图中的一个文件,而是我在此项目中的所有用户控件/窗口文件中以类似方式在 xaml 中添加的所有引用。

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.

所以文件在那里,文件中的命名空间是正确的,xaml 文件中的命名空间/类名(据我所知)是正确的。当我输入 xaml 时,我得到了智能感知,所以它发现文件正常,但在编译时却没有。

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.

其他帖子中最常见的解决方案是 .net 框架版本。我的主项目和测试项目目前都设置为 .Net Framework 4。完整版不是客户资料。

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.

这是我认为我搞砸的事情:在配置管理器中,两个项目的平台都设置为 Any CPU,但在尝试解决此问题时,我注意到主项目设置为 x86,测试项目设置为 Any中央处理器。所以我在配置管理器中为主项目手动添加了 Any CPU。但是,老实说,我不知道我是否正确地做到了这一点,或者我是否应该这样做。那么作为一个附加问题,有没有办法将配置管理器重置为其默认状态?这对主要问题有什么要说的吗?我不知道主项目是否总是设置为 x86 或者我是否以某种方式将其更改为 x86 然后它坏了。如前所述,这个项目编译了一段时间就好了。

采纳答案by gil kr

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

每次发生在我身上时,我都只是重新启动了visual studio,重新构建了解决方案,并且效果很好.. 说不出为什么

回答by WiiMaxx

  • 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 Classis public
  • 我会建议重命名 x:Key="ViewModel"也许有一个小故障
  • 如果你输入local:VS 会显示吗HistoryViewModel
  • 还要检查你Class是否是public

回答by Jerry

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.

除了“命名空间中不存在”消息之外,我还从设计器那里收到一条消息,说它无法显示 x64 和 ARM 目标的窗口。

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.

我刚刚发现将构建切换到 x86 模式,执行重建解决方案,然后切换回 x64 模式,然后再次重建修复 [两个] 问题。

Simply rebuilding the x64 solution did nothing.

简单地重建 x64 解决方案没有任何作用。

回答by Leon Storey

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

这就是在 Visual Studio 2012(更新 3)上对我有用的方法。

  • Restart Visual Studio
  • Add current assembly to namespace declaration xmlns:framework="clr-namespace:TimeRecorder.Framework;assembly=MyAssembly
  • Build-> Build Solution
  • 重新启动 Visual Studio
  • 将当前程序集添加到命名空间声明 xmlns:framework="clr-namespace:TimeRecorder.Framework;assembly=MyAssembly
  • Build-> Build Solution

回答by Hugues

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

对我有用的方法: - 将解决方案配置从 Debug 切换到 Release - 将配置从 Release 切换回 Debug

回答by John C

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.

重建您的解决方案(有时清理然后构建效果更好)。然后查看您的错误列表,滚动到最底部,它很可能表示不允许您的程序集编译的错误,并且 XAML 编译器很可能使用的是程序集的缓存版本,而不是您使用的新版本建造的意思。

回答by Chandru

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 HistoryViewModelclass )
  • Once its built, add the referencing markup in xaml
  • build again
  • 从 xaml 中删除引用标记(在本例中为<local:HistoryViewModel x:Key="ViewModel"/>
  • 构建类(在这个包含HistoryViewModel类的示例文件中)
  • 构建完成后,在 xaml 中添加引用标记
  • 重新构建

The above method worked for me.

上述方法对我有用。

回答by Noxxys

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
  • 从 References 中删除库的 dll
  • 下载库的源代码(而不仅仅是dll文件)
  • 构建库的项目以获取新的dll文件
  • 将新的dll文件添加到主项目的References

回答by Moon Waxing

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.

有这个问题绕圈子浪费了几个小时。我将一个单独的用户控件 dll 移动到项目中,因此它是在项目中编译的,而不是引用的 dll。这破坏了整个项目,所以我仔细检查了所有的命名空间、路径和文件名。尝试删除 obj 文件,在发布和调试之间,在 x86 和 AnyCPU 之间更改。打开保存所有,重新编译仍然没有乐趣。

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

记得之前有过类似的问题,VS2013 中标记的错误与我必须修改 XAML 的位置没有直接关系,而是通过使用

x:Name="myControl"

on all controls, instead of

在所有控件上,而不是

Name="myControl"

fixed it.

修复。

回答by André Santaló

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 issueand it stated the following:

这对我来说是一个反复出现的问题。有一次我找到了查看警告选项卡的解决方案。这是一个.NET 框架版本问题,它说明了以下内容:

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".

警告 9 无法解析主要引用“myDll”,因为它是针对“.NETFramework,Version=v4.5.2”框架构建的。这是比当前目标框架“.NETFramework,Version=v4.0”更高的版本。