2014-10-17 38 views
1

我嘗試在WPF應用程序中使用Acrobat PDF閱讀器,但發現WindowsFormsHost是WinForm控件...因此它可能是問題的根源... I在指定的行中收到消息「無法將OphtalBox.PDFReader轉換爲Windows.Forms.control」。由於無法將我的UserControl轉換爲Windows.Forms.Control

我做這2個教程的混合:
​​

http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app

我的頁面,以顯示我的用戶

public partial class DidactielPage : Window 
{ 
    public DidactielPage() 
    { 
     InitializeComponent(); 
     var ucPdfReader = new PdfReader("/Resource/Data/DidacticielOphtalBoX.pdf"); 
     this.WindowsFormHost1.Child = ucPdfReader;// the error message shows here 


    } 
} 

我的用戶

public partial class PdfReader : UserControl 
{ 
    public PdfReader(string filename) 
    { 
     InitializeComponent(); 

     AcroPDF acro = new AcroPDF(); 
     acro.setShowToolbar(false); 

     acro.setView("FitH"); 
     acro.LoadFile(filename); 
     acro.src = filename; 
     acro.setViewScroll("FitH", 0); 
    } 
} 
+0

當您的PdfReader是WPF UserControl時,爲什麼要使用WindowsFormsHost?這沒有意義。 – Clemens 2014-10-17 11:35:59

+0

那麼有什麼意義?因爲這是在WPF教程中完成的工作:http://www.screencast.com/t/JXRhGvzvB – 2014-10-17 11:39:10

+0

將PdfReader直接放入其中一個常用的WPF面板中,例如,一個網格? – Clemens 2014-10-17 11:49:08

回答

0

您需要的是Windows窗體與元素主機的集成。

1)添加對WindowsFormsIntegration的引用,在添加引用對話框中,轉到.NET並按字母順序查找。

2)添加導入/使用

using System.Windows.Forms.Integration; 

3)使用這個可愛的小簡便方法。

private static ElementHost createFormHostForWpfElement(UserControl wpfControl) 
    { 
     ElementHost elementHost = new ElementHost(); 
     elementHost.Child = wpfControl; 
     return elementHost; 
    } 

4)現在將HostElement添加到您的表單中。

this.WindowsFormHost1.Child = createFormHostForWpfElement(ucPdfReader); 

讓我知道是否爲您照顧它。

0

遲到響應:要解決此問題,您應該使用System.Windows.Forms.Integration.ElementHost()庫中的ElementHost來包含UserControl,然後將ElementHost添加到子項目列表中。

嘗試以下操作:

var elementHostPartial = new System.Windows.Forms.Integration.ElementHost(); 
elementHostPartial.TabIndex = 0;//increment this if more controls are needed 
var ucPdfReader = new PdfReader("/Resource/Data/DidacticielOphtalBoX.pdf"); 
elementHostPartial.Child = ucPdfReader; 
this.WindowsFormHost1.Child = elementHostPartial; 

我希望這有助於。

相關問題