不能将类型“System.Windows.Forms.Control”隐式转换为“对象”

本文关键字:转换 对象 Forms 类型 System Windows 不能 Control | 更新日期: 2023-09-27 18:32:11

我正在演练 WPF 演练:在 WPF 中承载 Windows 窗体复合控件。

基本上,它首先使用 Windows 窗体创建一个用户控件,然后将其添加到 WPF 应用程序中。

我已经完成了用户控件,现在我正在实现文件隐藏的代码。由于它只是从 MSDN 链接复制和粘贴代码,因此让我们看一下该方法。

private void Init(object sender, EventArgs e)
    {
        app = System.Windows.Application.Current;
        myWindow = (Window)app.MainWindow;
        myWindow.SizeToContent = SizeToContent.WidthAndHeight;
        wfh.TabIndex = 10;
        initFontSize = wfh.FontSize;
        initFontWeight = wfh.FontWeight;
        initFontFamily = wfh.FontFamily;
        initFontStyle = wfh.FontStyle;
        initBackBrush = (SolidColorBrush)wfh.Background;
        initForeBrush = (SolidColorBrush)wfh.Foreground;
        (wfh.Child as MyControl1).OnButtonClick += new MyControl1.MyControlEventHandler(Pane1_OnButtonClick);
        UIIsReady = true;
    }

有一个错误

无法将类型"System.Windows.Forms.Control"隐式转换为"对象"

在代码:

(wfh.Child as MyControl1).OnButtonClick += new MyControl1.MyControlEventHandler(Pane1_OnButtonClick);

如何纠正?

不能将类型“System.Windows.Forms.Control”隐式转换为“对象”

终于自己想通了,我得加一个dll

System.Windows.Forms

到 WPF 项目,但 MSDN 没有提到它。