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