简体   繁体   English

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

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

I've run into an issue with the Microsoft.Win32.SaveFileDialog in our Wpf application.我在 Wpf 应用程序中遇到了Microsoft.Win32.SaveFileDialog的问题。

If the user enters an enormous file path, above the allowed maximum (I think its 255 chars?), within the SaveFileDialog then it starts to become unusable (details of this are in the code example).如果用户在SaveFileDialog中输入了一个巨大的文件路径,超过了允许的最大值(我认为它的 255 个字符?),那么它开始变得不可用(详细信息在代码示例中)。

So as a work-around I want to close the dialogue and make them enter the file path again.因此,作为一种解决方法,我想关闭对话并让他们再次进入文件路径。 However the problem is that the SaveFileDialog does not have a Close() routine or anything else that I can see to close it.但是问题是SaveFileDialog没有Close()例程或我可以看到的任何其他关闭它的程序。 How can I close the dialogue programmatically?如何以编程方式关闭对话?

// 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);
}

EDIT编辑

Thanks for you answers/comments.感谢您的回答/评论。 I've just tested this on my Windows 7 box and it behaves as expected so this maybe an issue only on XP.我刚刚在我的 Windows 7 盒子上测试了它,它的行为符合预期,所以这可能只是 XP 上的问题。

In WPF 4.0 on Windows 7 the SaveFileDialog is showing its own error dialog:在 Windows 7 上的 WPF 4.0 中,SaveFileDialog 显示了自己的错误对话框:

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

with an OK button to dismiss the error dialog.使用 OK 按钮关闭错误对话框。 That leads the user back to the original SaveFileDialog where they can change their value or Cancel.这会将用户带回到原来的 SaveFileDialog,他们可以在其中更改其值或取消。

For earlier versions where the behavior might be different, you can use the Windows UI Automation framework to programmatically click the 'Cancel' button on the SaveFileDialog.对于行为可能不同的早期版本,您可以使用Windows UI 自动化框架以编程方式单击 SaveFileDialog 上的“取消”按钮

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

Try setting the dialog result to close the dialog.尝试设置对话框结果以关闭对话框。

Send the window a WM_SYSCOMMAND message with a wParam parameter of SC_CLOSE.向 window 发送WM_SYSCOMMAND 消息,其中 wParam 参数为 SC_CLOSE。 This is the equivalent of clicking on the Close button in the upper-right corner of the dialog.这相当于单击对话框右上角的关闭按钮。

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

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