在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 } }
我不能长时间处理它,我没有时间了.如何解决这个问题?
好吧,你不能在Form
里面添加一个(TopLevelControl).你必须先把它作为儿童控制.如果设置有效,TopLevel
则设置为false.
VideoStream.TopLevel = false;
注意:WindowsFormsHost
即使使用Winforms应用程序,您也不能添加Form
内部a,Form
除非TopLevel
设置为false.