簡體   English   中英

無法將我的UserControl轉換為Windows.Forms.Control

[英]Cannot convert my UserControl to Windows.Forms.Control

我嘗試在WPF應用程序中使用Acrobat PDF Reader,但是我發現WindowsFormsHost是WinForm控件...因此它可能是問題的根源...我收到消息“無法將OphtalBox.PDFReader轉換為Windows.Forms。控制”。 謝謝

我混合了這兩個教程:
http://www.screencast.com/t/JXRhGvzvB

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


    }
}

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

您需要的是Windows Forms Integration與元素主機。

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

讓我知道是否可以幫您解決這個問題。

晚響應:要解決這個問題,你應該使用一個ElementHostSystem.Windows.Forms.Integration.ElementHost()庫包含用戶控件,然后添加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;

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM