[英]WebKitBrowser with WPF
I am trying to use the WebKitBrowser
with a WPF application, but am getting a runtime error that says: 我正在尝试将
WebKitBrowser
与WPF应用程序一起使用,但遇到运行时错误提示:
FileNotFoundException was unhandled by user code:
用户代码未处理FileNotFoundException:
An exception of type 'System.IO.FileNotFoundException' occurred in WpfApplication1.exe but was not handled in user code
WpfApplication1.exe中发生类型为'System.IO.FileNotFoundException'的异常,但未在用户代码中处理
Additional information: Could not load file or assembly 'WebKit.Interop, Version=533.0.0.0, Culture=neutral, PublicKeyToken=b967213f6d29a3be' or one of its dependencies.
附加信息:无法加载文件或程序集'WebKit.Interop,版本= 533.0.0.0,区域性=中性,PublicKeyToken = b967213f6d29a3be'或其依赖项之一。 The system cannot find the file specified.
该系统找不到指定的文件。
The exception is occurring where I am creating an instance of the WebKitBrowser
(inside the constructor of my MainWindow.xaml.cs
file): 在创建
WebKitBrowser
的实例(在MainWindow.xaml.cs
文件的构造函数内部)的地方发生了异常:
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
WebKit.WebKitBrowser browser = new WebKit.WebKitBrowser();
browser.Navigate("http://www.google.com");
host.Child = browser;
browser.Show();
grdBrowserHost.Children.Add(host);
I have the required using
statements at the top of my .cs
file: 我的
.cs
文件顶部有必需的using
语句:
using System.Windows.Forms;
using WebKit;
I have surrounded that code with a try-catch
statement, but when I then run my code, the WebKitBrowser
is not added to the window as I want it to be, so I'm guessing that the exception is being caught/ handled- though I don't see the Console.WriteLine();
我已经用
try-catch
语句包围了该代码,但是当我运行我的代码时, WebKitBrowser
并没有按我希望的那样添加到窗口中,因此我猜测正在捕获/处理了异常-尽管我没有看到Console.WriteLine();
message that I added to my try-catch
statement. 我添加到
try-catch
语句中的消息。
I have tried catching a FileNotFoundException
, or an Exception
, but with the same result- the application runs in exactly the same way as it did before I added the code to create an instance of the WebKitBrowser
. 我尝试捕获
FileNotFoundException
或Exception
,但是结果相同-应用程序的运行方式与添加代码创建WebKitBrowser
实例之前WebKitBrowser
。
grdBrowserHost
is the name that I gave to the <grid></grid>
XML element where I want to display the browser, although I have not written anything inside that XML element- do I need something there to be able to display the browser? grdBrowserHost
是我为要在其中显示浏览器的<grid></grid>
XML元素赋予的名称,尽管我没有在XML元素中编写任何内容-我是否需要一些东西才能显示浏览器? That part of my XML is: 我的XML的那部分是:
<TabItem Header="Browser">
<StackPanel>
<Grid x:Name="grdBrowserHost">
</Grid>
</StackPanel>
</TabItem>
Edit 编辑
Although I will want to be using the WebKit
browser, or another browser that supports JavaScript, etc, I've had a look into using the 'built in' web browser provided by WPF, and have edited my XAML to include the following markup inside the <StackPanel></StackPanel>
tags where I want to display the browser: 尽管我将要使用
WebKit
浏览器或其他支持JavaScript的浏览器等,但我已经使用WPF提供的“内置”网络浏览器进行了研究,并编辑了XAML以在其中包含以下标记我要在其中显示浏览器的<StackPanel></StackPanel>
标记:
<WebBrowser HorizontalAlignment="Left"
Height="100"
Margin="10, 10, 0, 0"
VerticalAlignment="Top"
Width="100"
Name="BuiltInBrowser" />
In the Design view in Visual Studio 2015, this markup shows a rectangular area on my application window where the browser will be displayed, however, when I run the application, the WebBrowser
is not displayed on the main window, even though the Design view in Visual Studio 2015 is showing a rectangular area (grey colouring) where the WebBrowser
is located. 在Visual Studio 2015的“设计”视图中,此标记在我的应用程序窗口上显示一个矩形区域,将在其中显示浏览器,但是,当我运行该应用程序时,即使“浏览器”中的“设计”视图未显示在主窗口上,也不会显示
WebBrowser
。 Visual Studio 2015显示了WebBrowser
所在的矩形区域(灰色)。
Why isn't the WebBrowser
being displayed on the application window when I run the application, even though it is being displayed in the Design view? 即使在“设计”视图中显示
WebBrowser
,为什么我在应用程序窗口中也没有显示该WebBrowser
?
How can I display a WebKitBrowser
here, rather than a WebBrowser
? 如何在此处显示
WebKitBrowser
而不是WebBrowser
?
If I try to open <WebKitBrowser></WebKitBrowser>
tags in my XAML, that line displays a compile error message that says: 如果我尝试在XAML中打开
<WebKitBrowser></WebKitBrowser>
标记,则该行将显示一条编译错误消息,内容为:
WebKitBrowser is not supported in a Windows Presentation Foundation (WPF) project
Windows Presentation Foundation(WPF)项目中不支持WebKitBrowser
How can I include/ reference WebKitBrowser
so that it is supported, and I can use it? 如何包含/引用
WebKitBrowser
,使其受支持,并且可以使用它?
The using statement only saves you typing the namespace repeatedly within that source file; using语句仅使您免于在该源文件中重复键入名称空间的麻烦。 You'll have added a project reference to the Webkit.Interop (or to a COM class library which will generate an Interop file for you).
您将对Webkit.Interop(或将为您生成一个Interop文件的COM类库)添加一个项目引用。 That dll is required at runtime, hence your error.
该dll在运行时是必需的,因此会出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.