VS2019中WinForms Designer无法打开新建窗体求助
WinForm设计器在.NET Framework 4.8 SDK项目中报错“Object reference not set to an instance of an object”
我有一个基于.NET Framework 4.8的C# SDK风格项目,右键添加新WinForm后,设计器无法打开,仅显示错误:
Object reference not set to an instance of an object.
对应的调用栈如下:
at System.Resources.Tools.StronglyTypedResourceBuilder.Create(IDictionary resourceList, String baseName, String generatedCodeNamespace, String resourcesNamespace, CodeDomProvider codeProvider, Boolean internalClass, String[]& unmatchable) at System.Resources.Tools.StronglyTypedResourceBuilder.Create(IDictionary resourceList, String baseName, String generatedCodeNamespace, CodeDomProvider codeProvider, Boolean internalClass, String[]& unmatchable) at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.BuildType() at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.GetObjectType() at Microsoft.VisualStudio.Shell.Design.GlobalType.get_ObjectType() at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.get_Children() at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjectsForItem(ProjectItem item, GlobalObjectCollection oldObjects, GlobalObjectCollection newObjects, ITypeResolutionService typeResolver) at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjectsForItem(ProjectItem item, GlobalObjectCollection oldObjects, GlobalObjectCollection newObjects, ITypeResolutionService typeResolver) at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjectsForItem(ProjectItem item, GlobalObjectCollection oldObjects, GlobalObjectCollection newObjects, ITypeResolutionService typeResolver) at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjects(Project project) at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.GetGlobalObjectsCore(Project project, Type baseType) at Microsoft.VisualStudio.Shell.Design.GlobalObjectProvider.GetGlobalObjects(Project project, Type baseType) at Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(Type baseType) at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetTypeFromGlobalObjects(String name, Boolean throwOnError, Boolean ignoreCase) at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase) at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name) at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetRuntimeType(String typeName) at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetType(String typeName) at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetType(String typeName) at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.GetType(IDesignerSerializationManager manager, String name, Dictionary`2 names) at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.FillStatementTable(IDesignerSerializationManager manager, IDictionary table, Dictionary`2 names, CodeStatementCollection statements, String className) 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) --- End of stack trace from previous location where exception was thrown --- at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)
求解决办法让设计器正常加载WinForm。
可行的解决方案
修复强类型资源文件
从调用栈看问题出在资源生成环节,检查项目中Properties/Resources.resx及对应的Resources.Designer.cs:- 删除自动生成的
Resources.Designer.cs文件 - 右键
Resources.resx文件,选择「运行自定义工具」重新生成代码 - 检查资源文件中是否存在无效的引用或命名空间不一致的情况
- 删除自动生成的
切换为传统.NET Framework项目格式
SDK风格项目对WinForm设计器的兼容存在问题,编辑.csproj文件,替换为传统项目结构:<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{你的项目GUID}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>你的命名空间</RootNamespace> <AssemblyName>你的程序集名称</AssemblyName> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Drawing" /> <Reference Include="System.Windows.Forms" /> </ItemGroup> <ItemGroup> <Compile Include="Form1.cs"> <SubType>Form</SubType> </Compile> <Compile Include="Form1.Designer.cs"> <DependentUpon>Form1.cs</DependentUpon> </Compile> <EmbeddedResource Include="Form1.resx"> <DependentUpon>Form1.cs</DependentUpon> <SubType>Designer</SubType> </EmbeddedResource> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>替换后重新加载项目,再尝试打开设计器。
清理重建项目
- 执行「项目 -> 清理」操作
- 删除项目目录下的
bin和obj文件夹 - 重启Visual Studio
- 重新生成项目后打开设计器
检查Visual Studio组件
确保已安装.NET Framework 4.8开发工具和Windows Forms设计器组件,可通过Visual Studio安装程序的「单个组件」选项卡检查并补充安装。
内容的提问来源于stack exchange,提问作者Boris
相关产品推荐
相关产品推荐

