无法将我的UserControl转换为Windows.Forms.Control

时间:2014-10-17 11:13:22

标签: c# wpf winforms pdf user-controls

我尝试在WPF应用程序中使用Acrobat PDF Reader,但我发现WindowsFormsHost是一个WinForm控件...所以它可能是问题的根源...... 我得到了消息"无法将OphtalBox.PDFReader转换为Windows.Forms.control"在指定的行。感谢

我混合了这两个教程:
http://www.screencast.com/t/JXRhGvzvB

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

我的页面显示我的usercontrol

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


    }
}

我的userControl类

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

2 个答案:

答案 0 :(得分:1)

您需要的是与元素主机的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);

如果能为您处理,请告诉我。

答案 1 :(得分:0)

延迟回复:要解决此问题,您应该使用ElementHost库中的System.Windows.Forms.Integration.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;

我希望这会有所帮助。