Embed Firefox/Gecko in WPF/C#

2020-05-21 04:28发布

问题:

I want to embed the current Gecko in my WPF-Project. I know there is the possibility with the Winforms-Host and the Skybound-Gecko-Library.

But I do not use the standard wpf-theme for my application. It is another and the scrollbar of the control will not be styled. Furthermore, this is an old library which is designed for Firefox 3.

Which is the best library/strategy to use the current Gecko in WPF?

回答1:

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

paid: (Awesomium-based)

  • http://awesomium.com/ (is free for startups)
  • http://wpfchromium4.codeplex.com/ (uses awesomium)

free: (Chrome Embedded Framework-based)

  • https://github.com/chillitom/CefSharp (provides WinForms and WPF, but uses CEF1)
  • https://bitbucket.org/xilium/xilium.cefglue/wiki/Home (uses CEF3, and therefore supports Chrome's multi-process model, flash plugin, and WebGL)


回答2:

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


回答3:

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

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



回答4:

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.



回答5:

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.