Embed Firefox/Gecko in WPF/C#

前端 未结 5 1708
野的像风
野的像风 2021-02-03 12:30

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

5条回答
  •  旧时难觅i
    2021-02-03 12:52

    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

         
    
    

    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.

提交回复
热议问题