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();

 

完美解决

posted @ 2016-03-23 16:53  ldldavid  阅读(2741)  评论(0编辑  收藏  举报