Skip to content

NativeWebBrowser

ITAgnesmeyer edited this page Apr 25, 2024 · 3 revisions

The NativeWebBrowser control displays a WebBrowser.

This control has moved to Diga.NativeControls.WebBrowser

  • The control is based on Edge Chromium WebView2.
  • You can find a DOT-NET wrapper at the following address: Diga.WebView2
  • In order for the control to be used, Edge Chromium must be installed on the target system.
  • The control has the option of loading local files without HTTP-server.

With this it is also possible to load WebAssemblies.

The following example loads and runs a Blazor page without accessing a web server.

   class BrowserWindow : NativeWindow
    {
        private NativeWebBrowser _Browser;
        protected override void InitControls()
        {
            this.Text = "WebBrowser";
            this.Name = "BrowserWindow";
            this.StatusBar = true;
            this.IconFile = "Firmen_Emblem.ico";
            this.Width = 600;
            this.Height = 400;
            this._Browser = new NativeWebBrowser()
            {
                Width = this.Width,
                Height = this.Height,
                Url = "http://localhost:1",
                IsStatusBarEnabled = true,
                DefaultContextMenusEnabled = false,
                DevToolsEnabled = false,
                EnableMonitoring = true,
                MonitoringFolder = ".\\wwwroot",
                MonitoringUrl = "http://localhost:1/"
            };
            this._Browser.DocumentTitleChanged += OnDocumentTitleChanged;
            this._Browser.NavigationStart += OnNavigationStart;
            this._Browser.NavigationCompleted += OnNaviationCompleted;
            this._Browser.WebResourceRequested += OnWebResourceRequested;
            this.Controls.Add(this._Browser);
        }
        private void OnWebResourceRequested(object sender, WebResourceRequestedEventArgs e)
        {
            Debug.Print(e.Request.Uri);
        }

        private void OnNaviationCompleted(object sender, NavigationCompletedEventArgs e)
        {
            if (e.IsSuccess)
                this.Text = e.IsSuccess + "->" + this._Browser.DocumentTitle;
            else
                this.Text = "Navigation-Error=>" + e.GetErrorText();
         
        }
        private void OnNavigationStart(object sender, NavigationStartingEventArgs e)
        {
            this.Text = "Start-Navigate" + e.Uri;
        }
        private void OnDocumentTitleChanged(object sender, WebView2EventArgs e)
        {
            this.Text = this._Browser.DocumentTitle;
        }
        
        protected override void OnSize(SizeEventArgs e)
        {
            if (e.Width == 0) return;
            base.OnSize(e);
            this._Browser.Left = e.X;
            this._Browser.Top = e.Y;
            this._Browser.Width = e.Width;
            this._Browser.Height = e.Height;
            this._Browser.DoDock();
        }
    }

The following three properties control the local loading of pages:

  • EnableMonitoring => Enables local loading.
  • MonitoringFolder => Is the path to the file folder that contains the website.
  • MonitoringUrl => Is the address with which the page is loaded. It should be noted here that a / must follow at the end.

See the ConsoleCaller - Example => BrowserWindow.cs

Clone this wiki locally