如果你不看它,webbrowser不会导航

时间:2012-01-01 12:08:10

标签: c# wpf webbrowser-control navigateurl

我遇到了一个奇怪的问题。我有一个tabcontrol和3个标签。在每个标签上我都有一个webbrowser控件。他们都导航到一个网站。但它只会导航,如果您实际上正在查看webbrowser控件。因此,在任务栏或系统托盘上将其最小化,不要让它导航到网站。

为什么?我该如何改变这种行为?

[编辑]

这似乎只在我启动应用程序时发生。在“专注”或“看待”之后,这种情况不再发生了。

更多信息,导航发生在与UI线程不同的线程中。 [/编辑]

[3nd EDIT]

这是一个测试用例:

XAML代码:

<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>

以下是文件背后的代码:

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");
}

如何重现:

webbrowser_LoadCompleted内加一个断点。然后按下位于tabcontrol第一个标签页上的按钮。

不要进入下一个标签页,等待几秒钟,如15个左右。

然后转到tabitem2或3/4/5。你会看到页面刚加载并且webbrowser_LoadCompleted事件被触发了。

2 个答案:

答案 0 :(得分:1)

这是WPF中的代码片段。单击该按钮后,它会最小化应用程序,并在2秒后调用导航到所有浏览器,同时窗口最小化。无论窗口状态或标签焦点如何,页面都会加载到所有选项卡中。 确保在Navigate内调用Dispatcher.Invoke。除非您调用调度程序,否则无法从其他线程在WPF中进行UI更改。那可能是个问题。 我的示例在下面调用来自不同线程的导航。

<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 :(得分:0)

根据文档,似乎这是WPF中此控件的行为。