A similar post on using the Gecko browser via WPF can be found here:
http://www.technical-recipes.com/2017/using-the-gecko-web-browser-in-wpf/
In this post I extend these ideas a little so that the Gecko web browser, a WinForms control, can be used as part of the MVVM pattern. Some other modifications are necessary, such as incorporating ICommand implementations, and supervisor controlling patterns to get this thing running.
Step 1: Create a new WPF project
Create using a suitably recent version of .NET. In this example I use version 4.5.2.
![](https://i-blog.csdnimg.cn/blog_migrate/492f3fea0723a84b13af46fe6d598e62.jpeg)
Step 2: Install NuGet packages
Install Gecko.
In my example I install the Windows 32-bit version.
Select Tools > NuGet Package Manager > Package Manager Console
Run the following command:
1 | Install-Package Geckofx45 |
Install System.Windows.Interactivity
Select Tools > NuGet Package Manager > Package Manager Console
Run the following command:
1 | Install-Package System.Windows.Interactivity.WPF -Version 2.0.20525 |
Step 3: Add any other essential references
Right click your project folder and select Add References.
Make sure that System.Windows.Forms is already installed.
![](https://i-blog.csdnimg.cn/blog_migrate/82a3ad43ab13a90c7a2ed8cbb79a9f45.jpeg)
Select WindowsFormsIntegration as the reference to add:
![](https://i-blog.csdnimg.cn/blog_migrate/faa247beee49093a4120d6eb09750822.jpeg)
Also add references to the Gecko DLLs which are installed to the ‘packages’ folder on installing via NuGet:
![](https://i-blog.csdnimg.cn/blog_migrate/4faf51c6073f367920b60d21ec89b785.jpeg)
And also add system windows interactivity:
![](https://i-blog.csdnimg.cn/blog_migrate/00418406a6c18939f1e65d2c6bc25d3d.jpeg)
Step 4: Set the configuration to 32 bit
As shown in the Configuration Manager screenshot below:
![](https://i-blog.csdnimg.cn/blog_migrate/6c3d965f0cbfe191f28b316bea50518b.jpeg)
Step 5: Add event handling infrastructure
Add the following classes to your project:
EventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; namespace GeckoMvvm { public class EventArgs<T> : EventArgs { public EventArgs(T value) { Value = value; } public T Value { get ; private set ; } } } |
EventRaiser.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; namespace GeckoMvvm { public static class EventRaiser { public static void Raise( this EventHandler handler, object sender) { handler?.Invoke(sender, EventArgs.Empty); } public static void Raise<T>( this EventHandler<EventArgs<T>> handler, object sender, T value) { handler?.Invoke(sender, new EventArgs<T>(value)); } public static void Raise<T>( this EventHandler<T> handler, object sender, T value) where T : EventArgs { handler?.Invoke(sender, value); } public static void Raise<T>( this EventHandler<EventArgs<T>> handler, object sender, EventArgs<T> value) { handler?.Invoke(sender, value); } } } |
RelayCommand.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | using System; using System.Windows.Input; namespace GeckoMvvm { public class RelayCommand<T> : ICommand { private readonly Predicate<T> _canExecute; private readonly Action<T> _execute; public RelayCommand(Action<T> execute) : this (execute, null ) { _execute = execute; } public RelayCommand(Action<T> execute, Predicate<T> canExecute) { if (execute == null ) throw new ArgumentNullException(nameof(execute)); _execute = execute; _canExecute = canExecute; } public bool CanExecute( object parameter) { return (_canExecute == null ) || _canExecute((T)parameter); } public void Execute( object parameter) { _execute((T)parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } } public class RelayCommand : ICommand { private readonly Predicate< object > _canExecute; private readonly Action< object > _execute; public RelayCommand(Action< object > execute) : this (execute, null ) { _execute = execute; } public RelayCommand(Action< object > execute, Predicate< object > canExecute) { if (execute == null ) throw new ArgumentNullException(nameof(execute)); _execute = execute; _canExecute = canExecute; } public bool CanExecute( object parameter) { return (_canExecute == null ) || _canExecute(parameter); } public void Execute( object parameter) { _execute(parameter); } // Ensures WPF commanding infrastructure asks all RelayCommand objects whether their // associated views should be enabled whenever a command is invoked public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; CanExecuteChangedInternal += value; } remove { CommandManager.RequerySuggested -= value; CanExecuteChangedInternal -= value; } } private event EventHandler CanExecuteChangedInternal; public void RaiseCanExecuteChanged() { CanExecuteChangedInternal.Raise( this ); } } } |
Step 6: Implement the Supervisor Controlling Pattern
Create the IView interface to add our Gecko grid browser host:
IView.cs
1 2 3 4 5 6 7 8 9 | using System.Windows.Forms.Integration; namespace GeckoMvvm { public interface IView { void AddHost(WindowsFormsHost host); } } |
And then modify the code behind in your main window view.
Don’t worry about the things like MainWindowViewModel etc that we haven’t implemented yet, we will come on to that shortly.
MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Gecko; using System.Windows; using System.Windows.Forms.Integration; namespace GeckoMvvm { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, IView { public MainWindow() { InitializeComponent(); Xpcom.Initialize( "Firefox" ); (DataContext as MainWindowViewModel).View = this as IView; } public void AddHost(WindowsFormsHost host) { gridWeb.Children.Add(host); } } } |
Step 7: Update the MainWindow.xaml view
MainWindow.xaml
Step 8: Add the Main Window View Model class
MainWindowViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using Gecko; using System.Windows.Forms.Integration; using System.Windows.Input; namespace GeckoMvvm { public class MainWindowViewModel { private ICommand _windowLoadedCommand; public ICommand WindowLoaded { get { return _windowLoadedCommand ?? (_windowLoadedCommand = new RelayCommand( x => { DoStuff(); })); } } public IView View { get ; set ; } public void DoStuff() { WindowsFormsHost host = new WindowsFormsHost(); GeckoWebBrowser browser = new GeckoWebBrowser(); host.Child = browser; View.AddHost(host); browser.Navigate( "http://www.google.com" ); } } } |
Step 9: Try it!
Having followed the instructions faithfully the embedded browser should navigate to the chosen google page as shown:![](https://i-blog.csdnimg.cn/blog_migrate/bb45fbd8f11dd52b5082fbda4bf76b96.jpeg)
The complete package is available for download from here: