WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常


WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

在wpf中封装Com组件时,调用组件的方法时抛出异常System.Windows.Forms.AxHost+InvalidActiveXStateException的异常。

通过网上查询发现,除了实例化com组件,还要将该对象进行初始化。

添加如下代码后

System.Windows.Controls.Grid CTSGrid = new System.Windows.Controls.Grid();
AxAutoTest _autoTestClass = new AxAutoTest();
//[WPF承载windows组件必须用WindowsFormsHost]
System.Windows.Forms.Integration.WindowsFormsHost host = null;

host = new System.Windows.Forms.Integration.WindowsFormsHost();
// 控制器实例AxAutoTest的载体添加到一个容器中
host.Child = _autoTestClass;
((System.ComponentModel.ISupportInitialize)(_autoTestClass)).BeginInit();
CTSGrid.Children.Add(host);
((System.ComponentModel.ISupportInitialize)(_autoTestClass)).EndInit();

 

完美解决


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM