的FolderBrowserDialog - Win32Exception发生了 - 该参数不正确 [英] FolderBrowserDialog - Win32Exception occurred - The parameter is incorrect

查看:265
本文介绍了的FolderBrowserDialog - Win32Exception发生了 - 该参数不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用的FolderBrowserDialog从WPF是这样的:

 公共静态布尔BrowseFolder(出字符串FOLDERNAME)
{
使用(DLG System.Windows.Forms.FolderBrowserDialog =新System.Windows.Forms.FolderBrowserDialog())
{
VAR的结果= dlg.ShowDialog();
FOLDERNAME = dlg.SelectedPath;
返回结果== System.Windows.Forms.DialogResult.OK;
}
}

在使用Visual Studio 2010的关于异常中断我在的ShowDialog()调用关闭该对话框后得到一个异常。我很好奇,为什么发生这种情况。



例外:Win32Exception结果
消息:该参数不正确结果
堆栈跟踪:在MS.Win32.UnsafeNativeMethods.SetFocus(HandleRef的hWnd)



更新结果
我也试图明确设置父,但异常被抛出仍然

 变种W =新System.Windows.Interop.WindowInteropHelper(父); 
System.Windows.Forms.IWin32Window I =新WindowWrapper(w.Handle);
结果= dlg.ShowDialog(ⅰ);


解决方案

这是只是有点互操作污秽。 WPF的代码试图将焦点设置返回主窗口时,该对话框正在缩小。问题是,该对话框已禁用窗口,所以它不能接收焦点呢。 WPF是太急于改变焦点,不知道,否则有关该对话框的行为什么。其实没有什么不顺心。


I try to use the FolderBrowserDialog from WPF like this:

public static bool BrowseFolder(out string folderName)
{
   using (System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog())
   {
        var result = dlg.ShowDialog();
        folderName = dlg.SelectedPath;
        return result == System.Windows.Forms.DialogResult.OK;
   }
}

When using "break on exception" in Visual Studio 2010 I get an Exception after closing the Dialog in the ShowDialog() call. I'm curious why this occurs.

Exception: Win32Exception
Message: The parameter is incorrect
Stacktrace: at MS.Win32.UnsafeNativeMethods.SetFocus(HandleRef hWnd)

Update
I also tried to set the parent explicitly, but the exception was thrown nonetheless.

var w = new System.Windows.Interop.WindowInteropHelper(parent);
System.Windows.Forms.IWin32Window i = new WindowWrapper(w.Handle);
result = dlg.ShowDialog(i);

解决方案

This is just a bit of interop nastiness. The WPF code tries to set the focus back to the main window when the dialog is closing. Problem is, the dialog has disabled the window so it can't receive the focus yet. WPF is too eager to change the focus and doesn't otherwise know anything about the dialog behavior. Nothing actually goes wrong.

这篇关于的FolderBrowserDialog - Win32Exception发生了 - 该参数不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆