2014-10-17 62 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; 

我希望这有助于。