lundi 21 janvier 2019

Issues while recreating a webBrowser control in WPF

When I recreate a webBrowser control in WPF and then navigate to a page, sometimes the control is created successfully and the page loads, sometimes the control is not completely created and then page loads in the default browser associated with Windows (Firefox on my computer at the moment).

Here is how I delete and recreate the webBrowser:

 webBrowser.Dispose();
 webBrowser = null;

 webBrowser = new WebBrowser();
 tabItem_webBrowser.Content = webBrowser;
 webBrowser.HorizontalAlignment = HorizontalAlignment.Right;
 webBrowser.Height = 228;
 webBrowser.VerticalAlignment = VerticalAlignment.Top;
 webBrowser.Width = 586;
 webBrowser.RenderTransformOrigin = new Point(0.5, 0.5);
 webBrowser.Margin = new Thickness(0, 41, 10, 0);
 webBrowser.Navigated += SuppressScriptErrors;
 webBrowser.Visibility = Visibility.Visible;
 ScrollViewer.SetHorizontalScrollBarVisibility(webBrowser, ScrollBarVisibility.Visible);
 ScrollViewer.SetVerticalScrollBarVisibility(webBrowser, ScrollBarVisibility.Visible);
 webBrowser.LoadCompleted += WebBrowser_LoadCompleted;

I tried to see if I could check if the webBrowser was initialized with webBrowser.IsInitialized but the same problem occurs.

Is there a way to make sure that the webBrowser is fully created before doing a webBrowser.navigate(url)?

Note: The reason I am disposing and recreating it is to prevent memory leaks.

May you help me out with that please?

Thank you for your time and help, it is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire