繁体   English   中英

发生错误时如何关闭 Win32 SaveFileDialog?

[英]How to close Win32 SaveFileDialog when an error has occurred?

我在 Wpf 应用程序中遇到了Microsoft.Win32.SaveFileDialog的问题。

如果用户在SaveFileDialog中输入了一个巨大的文件路径,超过了允许的最大值(我认为它的 255 个字符?),那么它开始变得不可用(详细信息在代码示例中)。

因此,作为一种解决方法,我想关闭对话并让他们再次进入文件路径。 但是问题是SaveFileDialog没有Close()例程或我可以看到的任何其他关闭它的程序。 如何以编程方式关闭对话?

// error only seems to occur if a filter is specified.
var dialog = new Microsoft.Win32.SaveFileDialog 
{ 
    Filter = "My juicy file (*.mjf) | *.mjf" 
};

try
{
    dialog.ShowDialog();
}
catch (System.IO.PathTooLongException error) // handle
{
    /*
        * if I handle this exception (by displaying a message to the user)
        * the dialog will remain open, but if the user attempts to use it
        * and enter another filename a mixture of the following exceptions
        * are raised:
        * 
        * AccessViolationException
        * FatalExecutionEngineError
        * ExecutionEngineException
    */

    MessageBox.Show(error.Message);
}

编辑

感谢您的回答/评论。 我刚刚在我的 Windows 7 盒子上测试了它,它的行为符合预期,所以这可能只是 XP 上的问题。

在 Windows 7 上的 WPF 4.0 中,SaveFileDialog 显示了自己的错误对话框:

<long path?
The path is too long.
Try a shorter name.

使用 OK 按钮关闭错误对话框。 这会将用户带回到原来的 SaveFileDialog,他们可以在其中更改其值或取消。

对于行为可能不同的早期版本,您可以使用Windows UI 自动化框架以编程方式单击 SaveFileDialog 上的“取消”按钮

if (dialog != null)
{
    dialog.DialogResult = DialogResult.Cancel;
}

尝试设置对话框结果以关闭对话框。

向 window 发送WM_SYSCOMMAND 消息,其中 wParam 参数为 SC_CLOSE。 这相当于单击对话框右上角的关闭按钮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM