wpf Visual Studio 2012 XAML 设计器无效标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12871358/
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
Visual studio 2012 XAML designer invalid markup
提问by moreyummystuff
I've got a project that contains usercontrols. For some reason, the design view for the window which uses the said controls has stopped functioning and insists on "Invalid Markup" - the error given is that some members of the usercontrols are not recognized or not accessible. The project however compiles and executes just fine. Loading it up in VS2010, the editor works fine, no errors whatsoever - the same even in Expression Blend.
我有一个包含用户控件的项目。出于某种原因,使用上述控件的窗口的设计视图已停止运行并坚持“无效标记” - 给出的错误是无法识别或无法访问用户控件的某些成员。然而,该项目编译和执行得很好。在 VS2010 中加载它,编辑器工作正常,没有任何错误 - 即使在 Expression Blend 中也是如此。
So far I have tried the typical clean, rebuild, manually deleting stuff from obj, updating the .xaml and rebuilding, tried starting VS2012 with /resetuserdata and /resetsettings - the problem persists. I even rolled back to a much older revision of my project, back when the designer was working just fine, no dice - same error. I also tried launching VS2012 as a new windows user to avoid any stored appdata that might be lingering, again a no go.
到目前为止,我已经尝试了典型的清理、重建、从 obj 手动删除内容、更新 .xaml 和重建,尝试使用 /resetuserdata 和 /resetsettings 启动 VS2012 - 问题仍然存在。我什至回滚到我的项目的一个更旧的版本,当设计师工作得很好时,没有骰子 - 同样的错误。我还尝试以新的 Windows 用户身份启动 VS2012,以避免任何可能挥之不去的存储的应用程序数据,再次失败。
Is there some other sort of cache or something that I could manually delete?
是否有其他类型的缓存或我可以手动删除的内容?
回答by dansan
There's also an issue with the ShadowCache (used for rendering designer) getting out of sync with your XAML in VS2012. I just found this out, as I was having a horrible time with the following error with derived user controls:
在 VS2012 中,ShadowCache(用于渲染设计器)与 XAML 不同步也存在问题。我刚刚发现了这一点,因为我在派生用户控件出现以下错误时度过了一段可怕的时光:
The local property XXXX can only be applied to types that are derived from YYYYY.
本地属性 XXXX 只能应用于派生自 YYYYY 的类型。
The shadow cache is located in your user profile directory:
影子缓存位于您的用户配置文件目录中:
app data\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache
应用数据\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache
You can delete everything in there and restart VS2012. After that, things worked much better (for at least a while!)
您可以删除那里的所有内容并重新启动VS2012。在那之后,事情变得更好了(至少有一段时间!)
回答by pr0gg3r
Delete everything in ShadowCache
删除ShadowCache 中的所有内容
C:\Users\username\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\
C:\Users\username\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\
Copy-pasteable paths
可复制粘贴的路径
Just copy/paste one of these paths into Windows Explorer. %LOCALAPPDATA%is a Windows Environment Variableit will be replaced with the corresponding value when entered into Explorer(also works in Command Prompt)
只需将这些路径之一复制/粘贴到 Windows 资源管理器中。%LOCALAPPDATA%是一个Windows 环境变量,当进入资源管理器时它将被替换为相应的值(也适用于命令提示符)
VS2012
VS2012
%LOCALAPPDATA%\Microsoft\VisualStudio\11.0\Designer\ShadowCache\
%LOCALAPPDATA%\Microsoft\VisualStudio\11.0\Designer\ShadowCache\
VS2013
VS2013
%LOCALAPPDATA%\Microsoft\VisualStudio\12.0\Designer\ShadowCache\
%LOCALAPPDATA%\Microsoft\VisualStudio\12.0\Designer\ShadowCache\
VS2015
VS2015
%LOCALAPPDATA%\Microsoft\VisualStudio\14.0\Designer\ShadowCache\
%LOCALAPPDATA%\Microsoft\VisualStudio\14.0\Designer\ShadowCache\
ASP.NET
ASP.NET
If you develop ASP.NET also delete
如果开发ASP.NET也删
%LOCALAPPDATA%\Microsoft\WebsiteCache
%LOCALAPPDATA%\Temp\VWDWebCache
%LOCALAPPDATA%\Microsoft\WebsiteCache
%LOCALAPPDATA%\Temp\VWDWebCache
回答by Gooey Developer
I'm not sure this will fix all cases but this is what worked for me (all the time now).
我不确定这会解决所有情况,但这对我有用(现在一直)。
I have VS2012, using the Blend designer in the IDE, building an x64 application, when I would define a new ICommand and add the Command attribute, I would get the designer isn't supported in x64 error (I forget the exact warning). If I switch to x86, I'd get invalid XAML markup errors complaining the the ICommand wasn't recognized or was not accessible, no matter how many times I rebuilt in x64. If I switch to x86, build, then switch back to x64, voila, not problems with the designer.
我有 VS2012,在 IDE 中使用 Blend 设计器,构建 x64 应用程序,当我定义一个新的 ICommand 并添加 Command 属性时,我会得到 x64 错误中不支持设计器(我忘记了确切的警告)。如果我切换到 x86,无论我在 x64 中重建多少次,我都会收到无效的 XAML 标记错误,抱怨 ICommand 无法识别或无法访问。如果我切换到 x86,构建,然后切换回 x64,瞧,设计师没有问题。
Now I recall similar issues with VS2008 and Blend version 3 and 4. From what I've read in other posts, using Any CPU might also resolve the issue.
现在我想起了 VS2008 和 Blend 版本 3 和 4 的类似问题。从我在其他帖子中读到的内容来看,使用 Any CPU 也可能解决这个问题。
回答by SinhaOjas
May be you have added user controls which are not supported or you might have added some wrong parent-child relationship in your user controls . Try removing the suspected User Controls and rebuilding the project . It must fix the issue . Cheers
可能是您添加了不受支持的用户控件,或者您可能在用户控件中添加了一些错误的父子关系。尝试删除可疑的用户控件并重建项目。它必须解决这个问题。干杯
回答by Bob
A little late, but something else to consider: If your no-parameter constructor is doing some other work, like getting data that doesn't exist because you are in the designer, add this at the start of your constructor:
有点晚了,但要考虑其他事情:如果您的无参数构造函数正在执行其他一些工作,例如获取由于您在设计器中而不存在的数据,请在构造函数的开头添加:
if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject())) return;
// Do work.
This is necessary because the designer does execute your no-parameter constructor which may fail in design mode an give you the invalid markup.
这是必要的,因为设计器确实执行了您的无参数构造函数,该构造函数可能在设计模式下失败并为您提供无效标记。
And, regardless of what others are saying, this is not a VS or WPF XAML Parser bug. I ran into this issue today using VS 2015, .Net 4.5.2.
而且,不管其他人怎么说,这不是 VS 或 WPF XAML 解析器错误。我今天在使用 VS 2015、.Net 4.5.2 时遇到了这个问题。
回答by Daniel
In Visual Studio 2013 this is a workaround I using for my Windows Phone Project, http://danielhindrikes.se/visual-studio/workaround-for-invalid-markup-problems-when-developing-for-windows-phone/. I guess it is the same designer that is used when you writing XAML for WPF.
在 Visual Studio 2013 中,这是我用于 Windows Phone 项目http://danielhindrikes.se/visual-studio/workaround-for-invalid-markup-problems-when-developing-for-windows-phone/的解决方法。我猜这与您为 WPF 编写 XAML 时使用的设计器相同。
Have tried to delete Shadow Cache but itjust work first time I start Visual Studio, above workaround is the only thing that helped me.
曾尝试删除阴影缓存,但它在我第一次启动 Visual Studio 时才起作用,上述解决方法是唯一对我有帮助的方法。
回答by H Rangel
I've the same problem while using controls with a custom (no param) Constructor.
我在使用带有自定义(无参数)构造函数的控件时遇到了同样的问题。
Check if the Custom/User Control constructor doesn't require any data that the WPF designer doesn't have. A quick wait to check that is surrounding the whole thing with a try catch, cleaning, rebuilding and then check to see if the problem is solver.
检查自定义/用户控件构造函数是否不需要 WPF 设计器没有的任何数据。快速等待以通过尝试捕获,清洁,重建来检查整个事情,然后检查问题是否已解决。
If it is not, try commenting everything except the default WPF Init cod, clean, rebuild and test it out.
如果不是,请尝试注释除默认 WPF Init cod 之外的所有内容,清理、重建并测试它。
回答by Bernd Schuhmacher
I have the same Problem with VS 2015 and 2017 but only if I compile for x64 only. Here it helps if I change compile target (Project properties - Build, its called Plattformziel in german) temporarily to x86, compile to project and switch back to x64.
我对 VS 2015 和 2017 有同样的问题,但前提是我仅针对 x64 进行编译。如果我将编译目标(项目属性 - 构建,在德语中称为 Plattformziel)临时更改为 x86,编译为项目并切换回 x64,这会有所帮助。
Maybe this is a "solution" for anybody else.
也许这是对其他人的“解决方案”。
回答by JMK
I just had the same problem (program compiling and running fine, XAML designer reporting invalid markup), the issue was that I had accidentally changed the assembly name of one of the projects in my solution, I changed it back and my XAML designer sprang back to life, so check the Properties page of each project in your solution, and ensure the Assembly Name is as you expect it to be.
我只是遇到了同样的问题(程序编译和运行正常,XAML 设计器报告无效标记),问题是我不小心更改了解决方案中一个项目的程序集名称,我改回来了,我的 XAML 设计器又跳了回来因此,请检查解决方案中每个项目的“属性”页面,并确保程序集名称符合您的预期。
回答by Enhakiel
You need to re:encode your xaml, open bugged files in ide -> save as -> save encoded (choose UTF8)
您需要重新:编码您的 xaml,在 ide 中打开被窃听的文件 -> 另存为 -> 保存编码(选择 UTF8)

