Embed Firefox/Gecko in WPF/C#
Asked Answered
R

5

10

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?

Rarebit answered 26/9, 2011 at 7:59 Comment(2)
Does it have to be FF or do you just want a proper browser?Prettify
it does not have to be ff, but would the best. a similar bunch of features like skybound gecko is ok (basic navigation and a mousemove-event to read the htmldom at the cursor). the IE is no alternative for meRarebit
P
8

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

paid: (Awesomium-based)

free: (Chrome Embedded Framework-based)

Prettify answered 26/9, 2011 at 10:14 Comment(5)
Question is about Gecko, not webkit/blinkStakhanovism
You're right but at the time i gave this answer there were no solutions using gecko. Rather then saying that I gave an alternative.Prettify
I did ask the OP before answering.Prettify
You should understand that it all gets indexed by google and misleading a lot of people searching for specific answer. Anyway, it's my opinion, don't want to fight over it:)Stakhanovism
@AlexLapa It's indexed by other engines too ;) Maybe the original poster is ok with answers about embedding Google Chrome but it's not really obvious when I read his/her initial question.Billowy
A
8

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);
Attending answered 16/12, 2016 at 13:26 Comment(1)
This is the only answer I see that addresses the question. From what I gather Gecko cannot be used in 'pure' WPF. I am happy to be corrected on this. You have to embed the WinForms-based Gecko browser using a WPF WindowsFormsHost.Interment
D
2

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

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

Dela answered 5/10, 2011 at 6:25 Comment(0)
I
1

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.

Interment answered 3/6, 2017 at 11:34 Comment(0)
R
0

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.

Rhynd answered 10/9, 2017 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.