在WindowsFormsHost中插入窗体时出现ArgumentException

Yod*_*oda 2 .net c# forms com wpf

在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.
Run Code Online (Sandbox Code Playgroud)

这是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
           }
        }
Run Code Online (Sandbox Code Playgroud)

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

Sri*_*vel 12

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

VideoStream.TopLevel = false;
Run Code Online (Sandbox Code Playgroud)

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