System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowDialog(this);
fd.ShowDialog(this);
上面的代码如果是在WinForm程序里面,是没有问题的。但是如果在WPF里面则编译不通过,错误描述是
The best overloaded method match for 'System.Windows.Forms.CommonDialog.ShowDialog(System.Windows.Forms.IWin32Window)' has some invalid arguments。
这是因为WPF的窗口不是继承IWin32Window接口的,继承的是System.Windows.IWindowService接口。如果想在WPF程序里面使用FolderBrowserDialog,可以象下面这么写:
private class OldWindow : System.Windows.Forms.IWin32Window
{
IntPtr _handle;
public OldWindow(IntPtr handle)
{
_handle = handle;
}
IWin32Window Members
}
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowNewFolderButton = false;
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
fd.ShowDialog(this);
{
IntPtr _handle;
public OldWindow(IntPtr handle)
{
_handle = handle;
}
IWin32Window Members
}
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowNewFolderButton = false;
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
fd.ShowDialog(this);