1

我正在用 C# 编写一个 GIS 应用程序。应用程序的一部分允许用户选择 KML 文件,然后程序将处理该文件。我正在使用OpenFileDialog,但问题是所有代码都在对话框关闭之前执行(并且在用户确定文件之后)。这需要相当长的时间,因为程序必须缩放并执行其他操作。有没有办法在我的代码执行之前以编程方式关闭对话框?

编辑:一些代码给那些问的人。

private void OnKMLFileSet(object sender, CancelEventArgs e)
{
    Polygon polygon = KmlToPolygon(openFileDialog2.FileName);
    // After this, I no longer need the file, but the dialog stays open until the end of the method
    Graphic graphic = new Graphic();
    graphic.Geometry = polygon;
    textBox1.Text = string.Format("{0:n}", CalculateAreaInSqKilometers(polygon)).Split('.')[0];
    textBox2.Text = string.Format("{0:n}", CalculateAreaInSqMiles(polygon)).Split('.')[0];
    textBox3.Text = string.Format("{0:n}", CalculateAreaInSqKnots(polygon)).Split('.')[0];
    Note polyInfo = new Note("Polygon with nautical area: " + textBox3.Text, polygon);
    map.Map.ChildItems.Add(polyInfo);
    map.ZoomTo(polygon.GetEnvelope());
}
4

1 回答 1

5

听起来对话框实际上已关闭,但它仍然“可见”,因为主窗口很忙并且尚未重新绘制自己。

一些想法:

  • 简单的方法:在对话框仍然可见的主窗体上调用Refresh()方法。总是在 ShowDialog 返回后立即调用它。
  • 如果加载需要相当长的时间,可能需要创建一个弹出“加载”对话框,可能带有一个取消按钮。使用BackgroundWorker类在后台线程中加载文件。工作人员完成后,加载文件并可以关闭弹出窗口。切记不要在没有适当同步的情况下从后台线程更改用户界面中的任何内容。

编辑:查看代码后,我想我看到了您的问题。您正在处理 FileOk 事件。这将产生您试图避免的效果。像这样使用对话框:

if (openFileDialog1.ShowDialog() == DialogResult.OK) {
    // open file
}

Don't use the FileOk event. I've never had reason to use it before... Also it might be helpful to follow the advice I already gave.

于 2011-08-12T14:09:45.420 回答