发生错误时如何关闭Win32 SaveFileDialog?
我在 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 上的问题。
I've run into an issue with the Microsoft.Win32.SaveFileDialog
in our Wpf application.
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).
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 7 上的 WPF 4.0 中,SaveFileDialog 显示其自己的错误对话框:
使用“确定”按钮来关闭错误对话框。这将导致用户返回到原始的 SaveFileDialog,他们可以在其中更改其值或取消。
对于行为可能不同的早期版本,您可以使用Windows UI自动化框架,用于以编程方式单击 SaveFileDialog 上的“取消”按钮。
In WPF 4.0 on Windows 7 the SaveFileDialog is showing its own error dialog:
with an OK button to dismiss the error dialog. That leads the user back to the original SaveFileDialog where they can change their value or Cancel.
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.
尝试设置对话框结果以关闭对话框。
Try setting the dialog result to close the dialog.
向窗口发送 WM_SYSCOMMAND 消息,其中包含SC_CLOSE 的 wParam 参数。这相当于单击对话框右上角的“关闭”按钮。
Send the window a WM_SYSCOMMAND message with a wParam parameter of SC_CLOSE. This is the equivalent of clicking on the Close button in the upper-right corner of the dialog.