如何在.NET CLI创建的项目中正确使用Windows Forms设计器?
问题:Visual Studio无法打开.NET 8 WinForms项目的设计器
我正在开发一个C#项目,日常使用VS Code编辑代码,通过.NET SDK的CLI工具构建。需要创建Windows Forms界面,希望借助Visual Studio的Windows Forms设计器进行可视化编辑,但由于项目并非由Visual Studio创建,遇到了设计器无法启动的问题。
通过Visual Studio的「文件>打开>项目/解决方案」打开.csproj文件后,双击窗体文件,设计器无法启动,弹出要求安装.NET Core 3.0的错误弹窗(实际项目基于.NET 8.0),同时Windows Forms输出以下控制台日志:
: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'. : Failed to run as a self-contained app. : - The application was run as a self-contained app because '\\?\C:\Users\<me>\AppData\Local\Microsoft\VisualStudio\17.0_f971214a\WinFormsDesigner\ycjrcxah.b4c\DesignToolsServer.runtimeconfig.json' did not specify a framework. : - If this should be a framework-dependent app, specify the appropriate framework in '\\?\C:\Users\<me>\AppData\Local\Microsoft\VisualStudio\17.0_f971214a\WinFormsDesigner\ycjrcxah.b4c\DesignToolsServer.runtimeconfig.json'.
我确认.csproj的框架配置正确,因为通过CLI SDK可以正常构建项目。该问题可通过基础的dotnet模板复现,步骤如下:
- 执行
dotnet new winforms - 在Visual Studio中打开生成的.csproj文件
- 在解决方案资源管理器中双击
Form1.cs
内容的提问来源于stack exchange,提问作者PGmath
相关产品推荐
相关产品推荐

