背景
我切换了操作系统,但我的C#表单应用程序运行正常,但无法正常运行。我在Windows上使用Visual Studio 2017,现在在Ubuntu上使用最新版本的MonoDevelop。
问题
我有一个主窗体,第二个窗体和第三个窗体,这些窗体应用户要求按此顺序打开。第三个窗口仅显示一个图表,除了调整大小/关闭之外,没有其他交互按钮。
运行第三个窗口后,其BeginInit()
方法将调用主窗体Dispose(bool)
方法,然后关闭主窗口,然后关闭第二个窗口,应用程序将挂起,直到我停止从monodevelop GUI调试为止。尽管我对MonoDevelop刚起步,所以我似乎找不到任何能指出发生这种情况的原因的信息,所以也许我没有找到合适的位置。
代码
主要形式:
// Open Second Window called "Diagnostics"
private void Plots_Click(object sender, EventArgs e)
{
Application.Run(new Diagnostics(xmlDirectory));
}
第二个“诊断”表格:
// Set up stuff and run the Third window called "ChartDisplay"
private void GenerateChart_Click(object sender, EventArgs e)
{
// irrelevant code
Application.Run(new ChartDisplay(datapoints, horizontalChoice, verticalChoice));
}
第三种“ ChartDisplay”表格:
public ChartDisplay(List<object[]> data, string xAxisLabel, string yAxisLabel)
{
InitializeComponent();
// irrelevant code that doesn't get run
}
ChartDisplay.Designer:
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); // last line to get called
// ...
}
调试
进入((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
中的Chart.Designer.cs
调用InitializeComponent()
会直接跳到前面提到的主要形式Dispose(bool)
方法。从那里进入所有内容只需循环执行Dispose函数,直到关闭主窗口,然后关闭所有其他窗口,使应用程序挂起。
由于篇幅太长,我将调用堆栈放在帖子的底部Dispose(bool)
上。
我尝试过的事情
关于相似问题,我唯一能找到的帮助是建议使用Application.Run(Form)
,而不是创建一个新的Form
对象并调用它的Show()
方法。如上所示,我切换到此方法,仍然没有骰子。
我尝试添加一种方法来防止窗户被关闭:
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
但是这什么也没做,甚至没有被调用。
单步执行并不能真正产生成果。看来我实际上无法进入引起BeginIinit()
在Dispose(bool)
处的调用堆栈
BitClean.MainWindow.Dispose(bool disposing) in /home/me/myapp/MainWindow.Designer.cs:15
System.ComponentModel.Component.Dispose() in
System.Windows.Forms.Form.WmClose(System.Windows.Forms.Message m) in
System.Windows.Forms.Form.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.Control.ControlWindowTarget.OnMessage(System.Windows.Forms.Message m) in
System.Windows.Forms.Control.ControlNativeWindow.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.NativeWindow.WndProc(System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) in
System.Windows.Forms.XplatUIX11.SendMessage(System.IntPtr hwnd, System.Windows.Forms.Msg message, System.IntPtr wParam, System.IntPtr lParam) in
System.Windows.Forms.XplatUI.SendMessage(System.IntPtr handle, System.Windows.Forms.Msg message, System.IntPtr wParam, System.IntPtr lParam) in
System.Windows.Forms.Form.Close() in
System.Windows.Forms.Application.Exit(System.ComponentModel.CancelEventArgs e) in
System.Windows.Forms.Application.Exit() in
System.Windows.Forms.Application.OnThreadException(System.NotImplementedException t) in
System.Windows.Forms.NativeWindow.OnThreadException(System.NotImplementedException e) in
System.Windows.Forms.NativeWindow.WndProc(System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) in
System.Windows.Forms.XplatUIX11.DispatchMessage(System.Windows.Forms.MSG msg) in
System.Windows.Forms.XplatUI.DispatchMessage(System.Windows.Forms.MSG msg) in
System.Windows.Forms.Application.RunLoop(bool Modal, System.Windows.Forms.ApplicationContext context) in
System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context) in
System.Windows.Forms.Application.Run(BitClean.Diagnostics mainForm) in
BitClean.MainWindow.Plots_Click(System.Windows.Forms.ToolStripMenuItem sender, System.Windows.Forms.MouseEventArgs e) in /home/me/myapp/MainWindow.cs:254
System.Windows.Forms.ToolStripItem.OnClick(System.Windows.Forms.MouseEventArgs e) in
System.Windows.Forms.ToolStripMenuItem.OnClick(System.Windows.Forms.MouseEventArgs e) in
System.Windows.Forms.ToolStripMenuItem.HandleClick(int mouse_clicks, System.Windows.Forms.MouseEventArgs e) in
System.Windows.Forms.ToolStripItem.FireEvent(System.Windows.Forms.MouseEventArgs e, System.Windows.Forms.ToolStripItemEventType met) in
System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs mea) in
System.Windows.Forms.ToolStripDropDown.OnMouseUp(System.Windows.Forms.MouseEventArgs mea) in
System.Windows.Forms.Control.WmLButtonUp(System.Windows.Forms.Message m) in
System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.ToolStrip.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.ToolStripDropDown.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.Control.ControlWindowTarget.OnMessage(System.Windows.Forms.Message m) in
System.Windows.Forms.Control.ControlNativeWindow.WndProc(System.Windows.Forms.Message m) in
System.Windows.Forms.NativeWindow.WndProc(System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) in
System.Windows.Forms.XplatUIX11.DispatchMessage(System.Windows.Forms.MSG msg) in
System.Windows.Forms.XplatUI.DispatchMessage(System.Windows.Forms.MSG msg) in
System.Windows.Forms.Application.RunLoop(bool Modal, System.Windows.Forms.ApplicationContext context) in
System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context) in
System.Windows.Forms.Application.Run(BitClean.MainWindow mainForm) in
BitClean.Program.Main() in /home/me/myapp/Program.cs:19