WPF:VS2012 设计器显示“组件‘XXX’没有由 URI YYY 标识的资源”

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

WPF: VS2012 designer shows "The component 'XXX' does not have a resource identified by the URI YYY"

wpfvisual-studiodesigner

提问by tete

As the title suggests, this error is thrown by the designer, which means the designer can't display my UserControl correctly which in turn means I can't navigate smoothly from element to element in this UserControl to make modification. Really annoying.

正如标题所暗示的那样,此错误是由设计器引发的,这意味着设计器无法正确显示我的 UserControl,这反过来意味着我无法在此 UserControl 中的元素之间平滑导航以进行修改。真的很烦。

XXX is the name of my UserControl, while the URI YYY is actually XXX's path. So I don't know understand it can't find itself as resource. I googled this error, but most of them happened in the runtime. In my case it doesn't happen at all when I execute it. The description of this error is really not explanatory enough, because I am not sure who in the CLR is loading the file itself as a resource.

XXX 是我的 UserControl 的名称,而 URI YYY 实际上是 XXX 的路径。所以我不知道它不能找到自己作为资源。我用谷歌搜索了这个错误,但其中大部分发生在运行时。在我的情况下,当我执行它时它根本不会发生。这个错误的描述确实不够解释,因为我不确定CLR中是谁将文件本身作为资源加载。

Another thing might be worth mentioning is, this error only happens after I build my application project, which the error UserControl resides in. After I click to clean the project, the designer can display the whole stuff (but obviously I can't clean the project every time before I make any change, since the building takes time)

另一件值得一提的事情是,这个错误只发生在我构建我的应用程序项目之后,错误 UserControl 驻留在其中。 单击以清理项目后,设计器可以显示整个内容(但显然我无法清理每次在我进行任何更改之前进行项目,因为建筑物需要时间)

回答by o_weisman

It is possible that the control's .g.cs or .g.i.cs file has been corrupted. Try to clean, close visual studio and restart it. I think that helped for me in several cases especially when I copy paste controls from one solution to another.

控件的 .g.cs 或 .gics 文件可能已损坏。尝试清理、关闭 Visual Studio 并重新启动它。我认为这在多种情况下对我有帮助,尤其是当我将粘贴控件从一种解决方案复制到另一种解决方案时。

回答by Hamed Zakery Miab

Just delete subfolders in

只需删除中的子文件夹

%LOCALAPPDATA%\Microsoft\VisualStudio\12.0\Designer\ShadowCache

%LOCALAPPDATA%\Microsoft\VisualStudio\12.0\Designer\ShadowCache

回答by Tom Deloford

If you refactor a UserControl down a lower level project that the UserControl was previously referencing then you will see this error.

如果您将 UserControl 向下重构为 UserControl 先前引用的较低级别项目,那么您将看到此错误。

Removing the assembly reference from the namespace fixes the issue.

从命名空间中删除程序集引用可解决此问题。

<UserControl xmlns:ui="clr-namespace:MyCompany.Core.UI;assembly=MyCompany.Core.UI"

should be

应该

<UserControl xmlns:ui="clr-namespace:MyCompany.Core.UI"

The designer is not smart enough to highlight this.

设计师不够聪明,无法突出这一点。

回答by Greg Gorman

To display the UserControl, the designer has to instantiate the user control. Look for possible Null Reference Exceptions.

要显示 UserControl,设计器必须实例化用户控件。寻找可能的空引用异常。

In my case the user control had an NRE due to a dependency not being injected. I added handling for the NRE and no more issue.

在我的情况下,由于未注入依赖项,用户控件具有 NRE。我为 NRE 添加了处理,没有更多问题。

回答by AXG1010

I ran into the same issue, it compiled and ran fine, but the XAML editor/Designer complained. In my case, I found the solution to be that my user control was declared within a namespace in the XAML (x:Class="myNamespace.myUserControl") but not in the code behind. Adding the namespace declaration in the code behind solved my issue.

我遇到了同样的问题,它编译并运行良好,但 XAML 编辑器/设计器抱怨。就我而言,我发现解决方案是我的用户控件是在 XAML (x:Class="myNamespace.myUserControl") 的命名空间中声明的,而不是在后面的代码中声明的。在后面的代码中添加命名空间声明解决了我的问题。

回答by Artru

There is a possibility to debug Visual Studio Designer in the second attached Visual Studio.

可以在第二个附加的 Visual Studio 中调试 Visual Studio 设计器。

See How to troubleshoot and debug Visual Studio design mode errorsfor details.

有关详细信息,请参阅如何对 Visual Studio 设计模式错误进行故障排除和调试