开发者

FolderBrowserDialog - Win32Exception occurred - The parameter is incorrect

开发者 https://www.devze.com 2023-01-10 23:25 出处:网络
I try to use the FolderBrowserDialog from WPF like this: public static bool BrowseFolder(out string folderName)

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.IWin3开发者_Go百科2Window 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.


I would try doing it without using the "using" statement, perhaps it is getting cleaned up too quickly.


I do belive you need a windows forms host to run anything from the Windows.Forms namespace... try it out!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号