如何在WPF中使用WebKit浏览器控件

时间:2022-11-19 09:59:04

I want to use WebKit browser control in my WPF application. However, I am not able to add it in design time. Even if I add the WebKit into toolbox it is disabled state.

我想在我的WPF应用程序中使用WebKit浏览器控件。但是,我不能在设计时添加它。即使我将WebKit添加到工具箱中,它也是禁用状态。

My question is how to use that control during design time on WPF form?

我的问题是如何在WPF窗体的设计期间使用该控件?

Thanks, Omkar

谢谢,Omkar

3 个解决方案

#1


2  

As explained in http://msdn.microsoft.com/en-us/library/ms742735(v=vs.110).aspx , you should first create a WPF grid in your XAML file.

正如http://msdn.microsoft.com/en-us/library/ms742735(v=vs.110).aspx中所解释的,您应该首先在XAML文件中创建一个WPF网格。

<Grid x:Name="grdBrowserHost">
</Grid>

And add a .NET WebKit in the WPF form by inserting this code into your C# source.

并在WPF表单中添加. net WebKit,将此代码插入到c#源代码中。

System.Windows.Forms.Integration.WindowsFormsHost host =new System.Windows.Forms.Integration.WindowsFormsHost();

// Create the ActiveX control.
WebKit.WebKitBrowser browser = new WebKit.WebKitBrowser();
browser.Navigate("http://www.google.com");

// Assign the ActiveX control as the host control's child.
host.Child = browser;

// Add the interop host control to the Grid 
// control's collection of child controls. 
grdBrowserHost.Children.Add(host);

Do not forget to include these references

不要忘记包括这些参考文献

System.Windows.Forms

WindowsFormsIntegration

in your project.

在您的项目。

Hope this help.

希望这个有帮助。

#2


0  

I'm guessing it's an activex control. Try this: http://msdn.microsoft.com/en-us/library/ms742735.aspx

我猜是activex控件。试试这个:http://msdn.microsoft.com/en-us/library/ms742735.aspx

#3


0  

One of the best approaches for hosting Webkit (or Blink) in a WPF application presently (as of October 2017) seems to be with the Awesomium project which has first-class wrapper libraries for various platforms, including WinForms and WPF.

目前(截至2017年10月)在WPF应用程序中托管Webkit(或Blink)的最佳方法之一似乎是Awesomium项目,该项目为包括WinForms和WPF在内的各种平台提供了一流的包装库。

I advise against using any web-browser control inside a System.Windows.Forms.Integration.WindowsFormsHost in a WPF application because, at least, of the added layers of indirection and unpredictable behaviour in High-DPI contexts - I'm sure there are other reasons too.

我建议不要在System.Windows.Forms.Integration中使用任何web浏览器控件。WPF应用程序中的WindowsFormsHost,至少是因为在高dpi上下文中添加了间接层和不可预知的行为——我肯定还有其他原因。

Note that Awseomium is not true open-source or Free Software. If your company makes more than $100k USD in profit per year then for commercial applications there is a $2900 USD per-title license fee.

注意,Awseomium不是真正的开源或免费软件。如果你的公司每年的利润超过10万美元,那么对于商业应用来说,每一项专利的许可费是29美元。

With that caveat out the way, getting started with Awesomium in WPF is straightforward:

有了这个警告,在WPF中开始使用Awesomium就很简单了:

  1. Download the Awesomium SDK. (As of October 2017 the Awesomium download page is unavailable for an upgrade, but the SDK is still available to download from the Internet Archive and other places online).
  2. 下载Awesomium SDK。(截至2017年10月,Awesomium下载页面无法进行升级,但SDK仍然可以从Internet Archive和其他在线地方下载)。
  3. The SDK installer will register the Awesomium assemblies in the GAC for you, so they'll appear in the WPF (and WinForms) Toolbox Add/Remove Items window.
  4. SDK安装程序将在GAC中为您注册Awesomium程序集,因此它们将出现在WPF(和WinForms)工具箱“添加/删除项目”窗口中。
  5. Open your WPF project in Visual Studio and open a XAML design document.
  6. 在Visual Studio中打开WPF项目并打开XAML设计文档。
  7. Open the Visual Studio Toolbox and open the Add/Remove Items window and add the Awesomeium WPF controls from the Awesomium.Windows.Controls.dll assembly loaded in your GAC. You'll probably only want the WebControl control (Awesomium.Windows.Controls.WebControl).
  8. 打开Visual Studio工具箱,打开Add/Remove Items窗口,并从Awesomium.Windows.Controls中添加Awesomeium WPF控件。在GAC中加载的dll程序集。您可能只想要WebControl控件(Awesomium.Windows.Controls.WebControl)。
  9. Drag and drop the control from the Toolbox onto your design surface - Visual Studio will automatically update your project to add the references to the Awesomeium assemblies.
  10. 将控件从工具箱拖放到设计表面上,Visual Studio将自动更新您的项目,以添加对Awesomeium程序集的引用。

Note there are other wrapper libraries around Chromium - I don't meant to endorse Awesomium specifically, but I had a good time with it when I used it in a recent project.

注意,在Chromium周围还有其他的包装库——我并不是要特别推荐Awesomium,但是我在最近的一个项目中使用它的时候非常开心。

Another popular library is CefSharp which is BSD licensed, which will be more palatable compared to Awesomium's, but (as far as I can tell) does not distribute an SDK installer which handles Visual Studio Toolbox integration for you - but it looks like you only need to manually add a reference to the CefSharp.Wpf.dll assembly and add the elements to your WPF project (using the XAML editor, not the Toolbox - which you should be doing anyway).

另一个流行的库是CefSharp BSD许可,这将是更容易Awesomium的相比,但(据我所知)不分发Visual Studio SDK安装程序处理工具箱集成——但看起来你只需要手动添加一个引用CefSharp.Wpf。组装并将元素添加到WPF项目中(使用XAML编辑器,而不是工具箱——无论如何您都应该这样做)。

#1


2  

As explained in http://msdn.microsoft.com/en-us/library/ms742735(v=vs.110).aspx , you should first create a WPF grid in your XAML file.

正如http://msdn.microsoft.com/en-us/library/ms742735(v=vs.110).aspx中所解释的,您应该首先在XAML文件中创建一个WPF网格。

<Grid x:Name="grdBrowserHost">
</Grid>

And add a .NET WebKit in the WPF form by inserting this code into your C# source.

并在WPF表单中添加. net WebKit,将此代码插入到c#源代码中。

System.Windows.Forms.Integration.WindowsFormsHost host =new System.Windows.Forms.Integration.WindowsFormsHost();

// Create the ActiveX control.
WebKit.WebKitBrowser browser = new WebKit.WebKitBrowser();
browser.Navigate("http://www.google.com");

// Assign the ActiveX control as the host control's child.
host.Child = browser;

// Add the interop host control to the Grid 
// control's collection of child controls. 
grdBrowserHost.Children.Add(host);

Do not forget to include these references

不要忘记包括这些参考文献

System.Windows.Forms

WindowsFormsIntegration

in your project.

在您的项目。

Hope this help.

希望这个有帮助。

#2


0  

I'm guessing it's an activex control. Try this: http://msdn.microsoft.com/en-us/library/ms742735.aspx

我猜是activex控件。试试这个:http://msdn.microsoft.com/en-us/library/ms742735.aspx

#3


0  

One of the best approaches for hosting Webkit (or Blink) in a WPF application presently (as of October 2017) seems to be with the Awesomium project which has first-class wrapper libraries for various platforms, including WinForms and WPF.

目前(截至2017年10月)在WPF应用程序中托管Webkit(或Blink)的最佳方法之一似乎是Awesomium项目,该项目为包括WinForms和WPF在内的各种平台提供了一流的包装库。

I advise against using any web-browser control inside a System.Windows.Forms.Integration.WindowsFormsHost in a WPF application because, at least, of the added layers of indirection and unpredictable behaviour in High-DPI contexts - I'm sure there are other reasons too.

我建议不要在System.Windows.Forms.Integration中使用任何web浏览器控件。WPF应用程序中的WindowsFormsHost,至少是因为在高dpi上下文中添加了间接层和不可预知的行为——我肯定还有其他原因。

Note that Awseomium is not true open-source or Free Software. If your company makes more than $100k USD in profit per year then for commercial applications there is a $2900 USD per-title license fee.

注意,Awseomium不是真正的开源或免费软件。如果你的公司每年的利润超过10万美元,那么对于商业应用来说,每一项专利的许可费是29美元。

With that caveat out the way, getting started with Awesomium in WPF is straightforward:

有了这个警告,在WPF中开始使用Awesomium就很简单了:

  1. Download the Awesomium SDK. (As of October 2017 the Awesomium download page is unavailable for an upgrade, but the SDK is still available to download from the Internet Archive and other places online).
  2. 下载Awesomium SDK。(截至2017年10月,Awesomium下载页面无法进行升级,但SDK仍然可以从Internet Archive和其他在线地方下载)。
  3. The SDK installer will register the Awesomium assemblies in the GAC for you, so they'll appear in the WPF (and WinForms) Toolbox Add/Remove Items window.
  4. SDK安装程序将在GAC中为您注册Awesomium程序集,因此它们将出现在WPF(和WinForms)工具箱“添加/删除项目”窗口中。
  5. Open your WPF project in Visual Studio and open a XAML design document.
  6. 在Visual Studio中打开WPF项目并打开XAML设计文档。
  7. Open the Visual Studio Toolbox and open the Add/Remove Items window and add the Awesomeium WPF controls from the Awesomium.Windows.Controls.dll assembly loaded in your GAC. You'll probably only want the WebControl control (Awesomium.Windows.Controls.WebControl).
  8. 打开Visual Studio工具箱,打开Add/Remove Items窗口,并从Awesomium.Windows.Controls中添加Awesomeium WPF控件。在GAC中加载的dll程序集。您可能只想要WebControl控件(Awesomium.Windows.Controls.WebControl)。
  9. Drag and drop the control from the Toolbox onto your design surface - Visual Studio will automatically update your project to add the references to the Awesomeium assemblies.
  10. 将控件从工具箱拖放到设计表面上,Visual Studio将自动更新您的项目,以添加对Awesomeium程序集的引用。

Note there are other wrapper libraries around Chromium - I don't meant to endorse Awesomium specifically, but I had a good time with it when I used it in a recent project.

注意,在Chromium周围还有其他的包装库——我并不是要特别推荐Awesomium,但是我在最近的一个项目中使用它的时候非常开心。

Another popular library is CefSharp which is BSD licensed, which will be more palatable compared to Awesomium's, but (as far as I can tell) does not distribute an SDK installer which handles Visual Studio Toolbox integration for you - but it looks like you only need to manually add a reference to the CefSharp.Wpf.dll assembly and add the elements to your WPF project (using the XAML editor, not the Toolbox - which you should be doing anyway).

另一个流行的库是CefSharp BSD许可,这将是更容易Awesomium的相比,但(据我所知)不分发Visual Studio SDK安装程序处理工具箱集成——但看起来你只需要手动添加一个引用CefSharp.Wpf。组装并将元素添加到WPF项目中(使用XAML编辑器,而不是工具箱——无论如何您都应该这样做)。