WPF中實例化Com組件,調用組件的方法時報System.Windows.Forms.AxHost+InvalidActiveXStateException的異常windows
在wpf中封裝Com組件時,調用組件的方法時拋出異常System.Windows.Forms.AxHost+InvalidActiveXStateException的異常。spa
經過網上查詢發現,除了實例化com組件,還要將該對象進行初始化。orm
添加以下代碼後對象
System.Windows.Controls.Grid CTSGrid = new System.Windows.Controls.Grid();
AxAutoTest _autoTestClass = new AxAutoTest();
//[WPF承載windows組件必須用WindowsFormsHost]
System.Windows.Forms.Integration.WindowsFormsHost host = null;it
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();io
完美解決容器