随笔 - 52  文章 - 0  评论 - 31  阅读 - 18万 
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
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(thisas System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win 
= new OldWindow(source.Handle);
fd.ShowDialog(
this);
复制代码

 

posted on   pdfw  阅读(10223)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示