VS2017 WinForms含用户控件的窗体设计器加载失败求助
What's Going On
I see you're hitting this frustrating issue in Visual Studio 2017 with VB.NET WinForms: every form that includes a user control throws this warning when opening the designer:
To prevent possible data loss before loading the designer, the following errors must be resolved:
With the underlying error message: Error HRESULT E_FAIL has been returned from a call to a COM component. Even creating a brand new user control and adding it to a fresh form doesn't fix it, and while the app runs perfectly fine, the designer refuses to load. You suspect ReSharper's unused reference cleanup or your laptop's VS2017 setup might be to blame, and provided this stack trace:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at Microsoft.VisualStudio.LanguageServices.Implementation.Utilities.Exceptions.ThrowEFail() at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.LookupTypeSymbol() at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.get_TypeKind() at EnvDTE.CodeTypeRef.get_TypeKind() at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.GetUrtTypeFromVsType(CodeTypeRef vsType) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnTypePopulateMembers(Object sender, EventArgs e) at System.CodeDom.CodeTypeDeclaration.get_Members() at Microsoft.VisualStudio.Design.Serialization.CodeDom.MergedCodeDomParser.CodeTypeDeclarationPopulator.OnPopulateMembers(Object sender, EventArgs e) at System.CodeDom.CodeTypeDeclaration.get_Members() at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration) at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
Solutions to Try
Based on the error details and common causes for this specific COM failure in the WinForms designer, here are actionable steps to fix it:
Roll back ReSharper's unused reference cleanup
ReSharper's cleanup tools sometimes remove implicit references that the WinForms designer relies on (even if your code doesn't call them directly).- Open your
.vbprojfile in a text editor and check for missing references likeSystem.DesignorSystem.Windows.Forms. If they're gone, add them back:<Reference Include="System.Design" /> <Reference Include="System.Windows.Forms" /> - Use ReSharper's undo feature to revert the cleanup, or adjust its settings to exclude designer-critical references from being removed.
- Open your
Reset Visual Studio's designer cache and settings
Corrupted cache files often cause designer weirdness.- Delete your project's
binandobjfolders, then delete the VS component model cache at%LOCALAPPDATA%\Microsoft\VisualStudio\15.0_xxxx\ComponentModelCache(replacexxxxwith your VS instance ID). - Reset VS settings via Tools > Import and Export Settings > Reset All Settings and restart VS.
- Delete your project's
Repair your Visual Studio 2017 installation
COM errors like this often stem from broken or missing VS components. Open the Visual Studio Installer, select your VS2017 install, click Modify, then choose the Repair option. This will fix any corrupted files or registered COM components related to the designer.Check your user control's designer code
Even if the app runs, the.Designer.vbfile for your user control might have hidden invalid type references. Open that file and scan for any undefined types, missing namespaces, or syntax errors that the compiler might overlook but the designer can't handle.Temporarily disable ReSharper
Pause ReSharper via Tools > ReSharper > Suspend ReSharper and try opening the designer again. If the error goes away, the issue is a conflict between ReSharper and the VS designer. Update ReSharper to the latest version compatible with VS2017, or tweak its code analysis settings to avoid interfering with the designer.
Why This Happens
Looking at the stack trace, the error occurs when VS's language service tries to parse the code DOM for the designer. This usually means there's a broken type reference somewhere—either from missing assemblies, corrupted cache, or ReSharper accidentally removing a reference the designer needs (even if your runtime code doesn't).
内容的提问来源于stack exchange,提问作者cbradley

