Webbrowser doesn't move unless you look at it

I have a strange problem. I have tabcontrol and 3 tabs. On each tab, I got a web browser control on it. They all go to the site. But it only works if you are really looking at a web browser control. Therefore, by minimizing the taskbar or systray, do not go to the website.

Why? How can I change this behavior?

[EDIT]

It is like I'm running an application. Once he has received a “focus” or “look,” this does not happen anymore.

Additional navigation information comes from a stream other than the UI stream. [/ EDIT]

[3rd EDIT]

Here is an example:

XAML Code:

<Window x:Class="WPFWebbrowserFocusTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="453" Width="755"> <Grid> <TabControl Height="390" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="709"> <TabItem Header="tabItem1" Name="tabItem1"> <Grid> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="18,17,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> </TabItem> <TabItem Header="tabItem2" Name="tabItem2"> <Grid> <WebBrowser Height="352" HorizontalAlignment="Left" Margin="0,6,0,0" Name="webBrowser1" VerticalAlignment="Top" Width="693" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" /> </Grid> </TabItem> <TabItem Header="tabItem3" Name="tabItem3"> <Grid> <WebBrowser Height="346" HorizontalAlignment="Left" Margin="6,6,0,0" Name="webBrowser2" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" /> </Grid> </TabItem> <TabItem Header="tabItem4" Name="tabItem4"> <Grid> <WebBrowser Height="346" HorizontalAlignment="Left" Margin="10,10,0,0" Name="webBrowser3" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" /> </Grid> </TabItem> <TabItem Header="tabItem5" Name="tabItem5"> <Grid> <WebBrowser Height="346" HorizontalAlignment="Left" Margin="10,10,0,0" Name="webBrowser4" VerticalAlignment="Top" Width="687" Navigated="webbrowser_Navigated" LoadCompleted="webbrowser_LoadCompleted" /> </Grid> </TabItem> </TabControl> </Grid> 

Here is the code behind the file:

 public MainWindow() { InitializeComponent(); } private void webbrowser_Navigated(object sender, NavigationEventArgs e) { this.SuppressScriptErrors((WebBrowser)sender, true); } private void webbrowser_LoadCompleted(object sender, NavigationEventArgs e) { WebBrowser wb = (WebBrowser)sender; if (e.Uri.AbsoluteUri != wb.Source.AbsoluteUri) return; } public void SuppressScriptErrors(System.Windows.Controls.WebBrowser wb, bool Hide) { FieldInfo fi = typeof(System.Windows.Controls.WebBrowser).GetField( "_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); if (fi != null) { object browser = fi.GetValue(wb); if (browser != null) { browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { Hide }); } } } private void button1_Click(object sender, RoutedEventArgs e) { this.webBrowser1.Navigate("http://www.google.com"); this.webBrowser2.Navigate("http://www.google.com"); this.webBrowser3.Navigate("http://www.google.com"); this.webBrowser4.Navigate("http://www.google.com"); } 

How to reproduce:

Place a breakpoint inside webbrowser_LoadCompleted . Then click the button located on the first tab of the tabcontrol.

Do not go to the next tab, wait a few seconds, for example 15 or so.

Then go to tabitem2 or 3/4/5. You will see that the page has just loaded and the webbrowser_LoadCompleted event webbrowser_LoadCompleted been fired.

+6
source share
2 answers

This seems to be the behavior of this control in WPF as documented.

0
source

Here's a snippet of code in WPF that works. As soon as you press the button, it minimizes the application, and after 2 seconds, calls will go to all browsers until the window is minimized. Pages are loaded into all tabs regardless of the state of the window or the focus of the tabs. Make sure you call Navigate inside Dispatcher.Invoke . You cannot make changes to the user interface in WPF from another thread unless you call the dispatcher. This can be a problem. My example below calls navigation from another thread.

 <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="MainWindow" Height="350" Width="525" StateChanged="Window_StateChanged"> <Grid> <TabControl Height="225" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="491"> <TabItem Header="tabItem1"> <WebBrowser Height="189" Name="webBrowser1" Width="479" /> </TabItem> <TabItem Header="tabItem2"> <WebBrowser Height="185" Name="webBrowser2" Width="466" /> </TabItem> <TabItem Header="tabItem3"> <WebBrowser Height="187" Name="webBrowser3" Width="434" /> </TabItem> </TabControl> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="116,268,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="236,268,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> </Grid> </Window> private void button1_Click(object sender, RoutedEventArgs e) { this.WindowState = System.Windows.WindowState.Minimized; } private void Window_StateChanged(object sender, EventArgs e) { if (this.WindowState == System.Windows.WindowState.Minimized) { new Thread((state) => { Thread.Sleep(TimeSpan.FromSeconds(2)); this.Dispatcher.Invoke(new Action(() => { webBrowser1.Navigate(textBox1.Text); webBrowser2.Navigate(textBox1.Text); webBrowser3.Navigate(textBox1.Text); }), null); }).Start(); } } 
+1
source

Source: https://habr.com/ru/post/904837/


All Articles