Embed Firefox/Gecko in WPF/C#

删除回忆录丶 提交于 2019-12-02 21:06:00
albertjan

You should have a look at these options, they all use Chromium:

paid: (Awesomium-based)

free: (Chrome Embedded Framework-based)

You can probably use WindowsFormsHost, tutorial here

https://nhabuiduc.wordpress.com/2014/09/18/geckofx-net-webbrowser-setup-and-features/

the interesting part is

WindowsFormsHost host = new WindowsFormsHost(); 
GeckoWebBrowser browser = new GeckoWebBrowser(); 
host.Child = browser; 
gridWeb.Children.Add(host);
jgauffin

WebKit.Net is free: http://sourceforge.net/projects/webkitdotnet/

Their GitHub page seems to have been more recently updated: https://github.com/webkitdotnet

Here is my answer. As stated by Roman, Gecko is Winforms-based, not WPF-based and so has to be incorporated via the WindowsFormsHost.

  1. After creating the Visual Studio project, install the Gecko package via NuGet, using the command: Install-Package Geckofx45

  2. Make sure the WindowsFormsIntegration and System.Windows.Forms references have been added to your project.

  3. In your Configuration Manager, set your configuration to 32-bit, to get rid of the compiler warnings.

  4. Update MainWindow.xaml 'Grid' element to give it a name and the handler for the 'Loaded' event

<Grid
    Name="GridWeb"
    Loaded="Window_Loaded">     
</Grid>

  1. Modify MainWindow.xaml.cs to incorporate the Gecko as well as make it navigate to a page on loading:

      public MainWindow()
      {
         InitializeComponent();
         Gecko.Xpcom.Initialize("Firefox");
      }
    
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         WindowsFormsHost host = new WindowsFormsHost();
         GeckoWebBrowser browser = new GeckoWebBrowser();
         host.Child = browser;
         GridWeb.Children.Add(host);
         browser.Navigate("http://www.google.com");
      }
    

I struggle using the SO code editor, so for more detailed explanations and screenshots, see this blog page.

This is an old question, but I came up with a pseudo-solution to add GeckoFX as a XAML tag such as:

<local:GeckoBrowser Width="400" Height="250" />

This can be accomplished by simply wrapping the whole thing in a UserControl such as:

XAML:

<UserControl x:Class="WpfApp1.Browser"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300">
    <Border x:Name="border" Background="Black" Margin="0"></Border>  
</UserControl>

C#:

public partial class Browser : UserControl
{
    WindowsFormsHost host = new WindowsFormsHost();
    GeckoWebBrowser browser = new GeckoWebBrowser();

    public Browser()
    {
        InitializeComponent();
        Xpcom.Initialize("Firefox");
        browser.Navigate("http://www.google.com");
        host.Child = browser;
        border.Child = host;
    }
}

Now, you can use the tag in WPF, in the same project where the UserControl exists.

I have been trying to get this to work as a Control in a library, so I can easily port it to any other project/solution, but it keeps giving me an error about mozglue.dll missing. I suspect this is due to the Xpcom.Initialize("Firefox") but I need to investigate further.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!