debugging 在不进行调试的情况下运行窗体时获取NullReferenceException|调试无异常

oalqel3c  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(78)

我想刷新一些编程技能,并开始了一个使用VS的C#项目,.NET 7.0。即使我只想编译这行代码

Form form = new Form();

我明白了

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.NativeWindow.AdjustWndProcFlagsFromConfig(Int32 wndProcFlags)
   at System.Windows.Forms.NativeWindow.get_WndProcFlags()
   at System.Windows.Forms.NativeWindow.get_WndProcShouldBeDebuggable()
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle, Boolean assignUniqueID)
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle)
   at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

但是当我运行debug时,它做了它应该做的事情。
重要的是要知道:首先,我必须引用System.Windows.Forms.dll,并使用NuGet安装System.Configuration.ConfigurationManger。
我做错了什么?
提前谢谢你Broda

uajslkp6

uajslkp61#

不要在.NET 6.0+项目中添加对System.Windows.Forms.dll的引用。这是. NETFramework引用Winforms的方式,现在您可能甚至引用了. NETFramework库(您引用的具体是什么?)
相反,在csproj顶部附近添加这一行(在PropertyGroup部分):

<UseWindowsForms>True</UseWindowsForms>

相关问题