6

我遇到了一个奇怪的问题。我有一个选项卡控件和 3 个选项卡。在每个选项卡上,我都有一个 webbrowser 控件。他们都导航到一个网站。但它只有在您实际查看 webbrowser 控件时才会导航。因此,在任务栏或系统托盘上将其最小化,不会使其导航到网站。

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

[编辑]

这似乎只在我启动应用程序时发生。在它得到“焦点”或“看看”之后,这种情况就不会再发生了。

更多信息,导航发生在与 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。然后按下位于 tabcontrol 第一个标签页上的按钮。

不要转到下一个标签页,等待几秒钟,比如 15 左右。

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

4

2 回答 2

1

这是 WPF 中有效的代码片段。单击按钮后,它将最小化应用程序,并在 2 秒后调用导航到所有浏览器,同时窗口最小化。无论窗口状态或选项卡焦点如何,页面都会加载到所有选项卡中。确保NavigateDispatcher.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();
    }
}
于 2012-01-01T12:43:16.153 回答
0

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

于 2012-03-27T20:23:39.153 回答