Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将我的UserControl转换为Windows.Forms.Control_C#_Wpf_Winforms_Pdf_User Controls - Fatal编程技术网

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

C# 无法将我的UserControl转换为Windows.Forms.Control,c#,wpf,winforms,pdf,user-controls,C#,Wpf,Winforms,Pdf,User Controls,我尝试在WPF应用程序中使用Acrobat PDF Reader,但我发现WindowsFormsHost是一个WinForm控件…因此它可能是问题的根源。。。 我在指示行收到消息“无法将OphtalBox.PDFReader转换为Windows.Forms.control”。谢谢 我混合了以下两个教程: 我的页面显示我的用户控件 公共部分课堂教学法第页:窗口 { 公共教学法页() { 初始化组件(); var ucPdfReader=newpdfreader(“/Resource/Data

我尝试在WPF应用程序中使用Acrobat PDF Reader,但我发现WindowsFormsHost是一个WinForm控件…因此它可能是问题的根源。。。 我在指示行收到消息“无法将OphtalBox.PDFReader转换为Windows.Forms.control”。谢谢

我混合了以下两个教程:

我的页面显示我的用户控件

公共部分课堂教学法第页:窗口
{
公共教学法页()
{
初始化组件();
var ucPdfReader=newpdfreader(“/Resource/Data/DidacticielOphtalBoX.pdf”);
this.WindowsFormHost1.Child=ucPdfReader;//此处显示错误消息
}
}
我的用户控制类

公共部分类PdfReader:UserControl
{
公共PdfReader(字符串文件名)
{
初始化组件();
AcroPDF acro=新的AcroPDF();
acro.setShowToolbar(错误);
acro.setView(“FitH”);
acro.LoadFile(文件名);
acro.src=文件名;
acro.setViewScroll(“FitH”,0);
}
}

您需要的是Windows窗体与element host的集成

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

请告诉我这是否能帮您解决问题。

延迟响应:要解决此问题,您应该使用
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;

我希望这有帮助。

当您的PdfReader是WPF用户控件时,为什么要使用WindowsFormsHost?那没有道理。那有什么意义呢?因为这就是WPF教程中所做的:将PdfReader直接放入一个常用的WPF面板中,例如网格?只是不要这样做。Adobe Reader在很多方面都很糟糕,你永远都不想支持它。谷歌“wpf pdf浏览器”去购物。