在WindowsFormsHost中插入窗体时出现ArgumentException

 Cy章健Mr_189 发布于 2023-02-06 15:37

在WPF应用程序中,我创建WindowsFormsHost并希望在其中插入Form包含COM/ Activex控件.但后来我得到:

A first chance exception of type 'System.ArgumentException' occurred in WindowsFormsIntegration.dll
   at System.Windows.Forms.Integration.WindowsFormsHost.set_Child(Control value)
   at HomeSecurity.MainWindow..ctor() w c:\Users\R\Documents\Visual Studio 2013\Projects\HomeSecurity\HomeSecurity\MainWindow.xaml.cs:row 26
'HomeSecurity.vshost.exe' (CLR v4.0.30319: HomeSecurity.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Symbols loaded.

这是MainWindow.的结构.类VideoStream扩展了类Form

      public MainWindow() {
           InitializeComponent();
           VideoStream VideoStream = new VideoStream();//creating a Form containing Activex control
           WindowsFormsHost Host = new WindowsFormsHost();//creating a host
           try {
               Host.Child = VideoStream;//setting content of Host, CAUSES THE EXCEPTION PRINTED ABOVE
           }catch(Exception e){
               Console.WriteLine(e.StackTrace);//output above
           }
        }

我不能长时间处理它,我没有时间了.如何解决这个问题?

1 个回答
  • 好吧,你不能在Form里面添加一个(TopLevelControl).你必须先把它作为儿童控制.如果设置有效,TopLevel则设置为false.

    VideoStream.TopLevel = false;
    

    注意:WindowsFormsHost即使使用Winforms应用程序,您也不能添加Form内部a,Form除非TopLevel设置为false.

    2023-02-06 15:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有