网络浏览器不导航,如果你不看它



我遇到了一个奇怪的问题。我有一个tabcontrol和3个标签。在每个标签上我都有一个网页浏览器控件。它们都指向一个网站。但它只有在你看网页浏览器控件时才会导航。因此,在任务栏或系统栏上最小化它,不会使它导航到网站。

为什么?我怎样才能改变这种行为?

[编辑]

这似乎只发生在我启动应用程序时。在它得到'焦点'或'看'后,这不再发生了。

更多信息,导航发生在不同的线程,而不是ui线程。(/编辑)

(美商编辑)

下面是一个测试用例:

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中放置一个断点。然后按下位于tab控件第一个tab页上的按钮。

暂时不要转到下一个选项卡,等几秒钟,比如15秒左右。

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

下面是在WPF中工作的代码片段。一旦你点击这个按钮,它就会最小化应用程序,并且在2秒后调用导航到所有浏览器,同时最小化窗口。无论窗口状态或选项卡焦点如何,都会在所有选项卡中加载页面。确保在Dispatcher.Invoke中调用Navigate。除非调用调度程序,否则不能从不同的线程更改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();
    }
}

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

相关内容

  • 没有找到相关文章

最新更新