In our OOB application, we are using a WebBrowser control. When running on the Mac, sometimes when navigating to different pages within the browser, the web pages or portions of them do not draw (a black rectangle is shown). Later, any regions after that
that get marked dirty, either through scrolling, resizing the browser, or in-page changes (like images coming in after a delay) get drawn normal. The site does not experience this issue in a regular Safari browser.
Does anyone know of how to prevent this from happening? Alternately, are there any suggestions for working around the bug?
I found a hacky workaround. For Mac builds, I created a timer that imperceptibly changes the margins of the WebBrowser periodically. This forces the browser to redraw, so the invalid rendering state gets fixed:
public partial class MyUserControl : UserControl
{
#if MAC
private Timer _redrawTimer;
#endif
public MyUserControl()
{
// Required to initialize variables
InitializeComponent();
#if MAC
bool toggle = false;
_redrawTimer = new Timer(
_ => {
Dispatcher.BeginInvoke(
() => Browser.Margin = new Thickness(
0, 0, 0, toggle ? 0.01 : 0));
toggle = !toggle;
},
null, 0, 1000);
#endif
}
}
Would you be willing to provide some additional details? I tried a simple web page and saw what was described (white screen, no content).
I would recommend a modification to my original code for forcing the web browser to redraw. We were getting hideous memory usage due to memory leaks in the Silverlight framework; every time the redraw happened, more memory leaked, and the redraw was being
triggered every second. That leak may or may not be fixed and may not be relevant to your application.
I can't remember exactly what we changed to get the redraw to happen for Mac, and I no-longer have access to that code. I'm contacting someone still at that company to post an update.
By the way, I think your code isn't working properly because your _redrawTimer is a local variable and can get garbage-collected at any time. I did the same thing originally, and it wasn't until I changed the timer to a private member of the class that
the timer worked properly.
Thanks for looking and the clarification. Too bad to hear about the memory leaks.
I might be ok because the implementation I'm looking at is for a quick "get the data and get off." I would do it all with a WebClient, however, the user needs to sign in to have their credentials established to get what I need. Once done, the webbrowser
wouldn't have to be used again.
Apoco
Member
60 Points
74 Posts
WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Aug 12, 2010 10:06 PM | LINK
In our OOB application, we are using a WebBrowser control. When running on the Mac, sometimes when navigating to different pages within the browser, the web pages or portions of them do not draw (a black rectangle is shown). Later, any regions after that that get marked dirty, either through scrolling, resizing the browser, or in-page changes (like images coming in after a delay) get drawn normal. The site does not experience this issue in a regular Safari browser.
Does anyone know of how to prevent this from happening? Alternately, are there any suggestions for working around the bug?
Apoco
Member
60 Points
74 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Aug 12, 2010 10:29 PM | LINK
I found a hacky workaround. For Mac builds, I created a timer that imperceptibly changes the margins of the WebBrowser periodically. This forces the browser to redraw, so the invalid rendering state gets fixed:
public partial class MyUserControl : UserControl { #if MAC private Timer _redrawTimer; #endif public MyUserControl() { // Required to initialize variables InitializeComponent(); #if MAC bool toggle = false; _redrawTimer = new Timer( _ => { Dispatcher.BeginInvoke( () => Browser.Margin = new Thickness( 0, 0, 0, toggle ? 0.01 : 0)); toggle = !toggle; }, null, 0, 1000); #endif } }thorwm
Member
44 Points
27 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Nov 20, 2010 03:14 PM | LINK
Apoco,
Would you be willing to provide some additional details? I tried a simple web page and saw what was described (white screen, no content).
Here's the code that places a webbrowser in the application:
namespace WebTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
webBrowser1.Navigate(new Uri("http://www.cnn.com"));
if (Environment.OSVersion.Platform.ToString() != "Win32NT")
{
Timer _redrawTimer;
bool toggle = false;
_redrawTimer = new Timer(
_ =>
{
Dispatcher.BeginInvoke(
() => webBrowser1.Margin = new Thickness(
0, 0, 0, toggle ? 0.01 : 0));
toggle = !toggle;
},
null, 0, 1000); }
}
}
}
FYI - The xaml says something like "this is a test!"
-- Hopefully you won't cringe too much when you see how I placed your code! :)
(and yes, I sent you an e-mail but I thought others may be experiencing the same problem!)
Thor
Apoco
Member
60 Points
74 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Nov 22, 2010 04:04 PM | LINK
I would recommend a modification to my original code for forcing the web browser to redraw. We were getting hideous memory usage due to memory leaks in the Silverlight framework; every time the redraw happened, more memory leaked, and the redraw was being triggered every second. That leak may or may not be fixed and may not be relevant to your application.
I can't remember exactly what we changed to get the redraw to happen for Mac, and I no-longer have access to that code. I'm contacting someone still at that company to post an update.
Apoco
Member
60 Points
74 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Nov 22, 2010 04:07 PM | LINK
By the way, I think your code isn't working properly because your _redrawTimer is a local variable and can get garbage-collected at any time. I did the same thing originally, and it wasn't until I changed the timer to a private member of the class that the timer worked properly.
thorwm
Member
44 Points
27 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Nov 23, 2010 01:54 AM | LINK
Thanks for looking and the clarification. Too bad to hear about the memory leaks.
I might be ok because the implementation I'm looking at is for a quick "get the data and get off." I would do it all with a WebClient, however, the user needs to sign in to have their credentials established to get what I need. Once done, the webbrowser wouldn't have to be used again.
Thor
bleiddyn
Member
16 Points
3 Posts
Re: WebBrowser control on Mac sometimes doesn't draw or draws partial web page
Feb 17, 2011 03:18 PM | LINK
My apologies for the lateness of this, but this would be the modifications for the finished code:
public partial class MyUserControl : UserControl { #if MAC private Timer _redrawTimer; #endif public MyUserControl() { // Required to initialize variables InitializeComponent(); #if MAC bool toggle = false; _redrawTimer = new Timer( _ => { Dispatcher.BeginInvoke( () => Browser.Margin = new Thickness( 0, 0, 0, toggle ? 0.01 : 0)); toggle = !toggle; }, null, Timeout.Infinite, Timeout.Infinite); #endif } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { #if MAC _redrawTimer.Dispose(); _redrawTimer = null; #endif } private void Browser_LoadCompleted(object sender, NavigationEventArgs e) { #if MAC _redrawTimer.Change(2000, Timeout.Infinite); #endif } }