Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果您';你没有看它_C#_Wpf_Webbrowser Control_Navigateurl - Fatal编程技术网

C# 如果您';你没有看它

C# 如果您';你没有看它,c#,wpf,webbrowser-control,navigateurl,C#,Wpf,Webbrowser Control,Navigateurl,我有个奇怪的问题。我有一个选项卡控件和3个选项卡。每个标签上都有一个webbrowser控件。它们都导航到一个网站。但它只有在您实际查看webbrowser控件时才会导航。因此,在任务栏或systray上将其最小化,不会使其导航到网站 为什么呢?我怎样才能改变这种行为 [编辑] 这似乎只有在我启动应用程序时才会发生。在“聚焦”或“注视”之后,这种情况就不再发生了 更多信息,导航发生在与UI线程不同的线程中。 [/编辑] [3nd编辑] 下面是一个测试用例: XAML代码: <Window

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

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

[编辑]

这似乎只有在我启动应用程序时才会发生。在“聚焦”或“注视”之后,这种情况就不再发生了

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

[3nd编辑]

下面是一个测试用例:

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>
如何复制:

webbrowser\u LoadCompleted
内放置断点。然后按下tabcontrol第一个选项卡页上的按钮

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


然后转到选项卡项2或3/4/5。您将看到页面刚刚加载,触发了
webbrowser\u LoadCompleted
事件。

以下是WPF中的代码片段。单击该按钮后,它将最小化应用程序,2秒钟后,调用将在最小化窗口的同时导航到所有浏览器。无论窗口状态或选项卡焦点如何,页面都会加载到所有选项卡中。 确保正在调度程序内调用
导航
。调用
。除非调用dispatcher,否则不能从其他线程在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\u单击(对象发送者,路由目标)
{
this.WindowState=System.Windows.WindowState.Minimized;
}
私有无效窗口\u状态已更改(对象发送方,事件参数e)
{
if(this.WindowState==System.Windows.WindowState.Minimized)
{
新线程((状态)=>
{
线程睡眠(TimeSpan.FromSeconds(2));
this.Dispatcher.Invoke(新操作(()=>
{
webBrowser1.导航(textBox1.Text);
webBrowser2.导航(textBox1.Text);
webBrowser3.导航(textBox1.Text);
}),空);
}).Start();
}
}

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

如果最小化它,它将如何导航?为什么在winforms中这不是一个问题?这不应该发生,无论
WebBrowser
控件在内存中没有聚焦、可见甚至只是实例化,它都应该始终导航到页面。您可以发布一些简化的xaml代码,然后如何调用这些
Navigate
方法吗@Yustme,这也不是WPF的问题。它是一个多线程应用程序,我从不同于UI线程的线程进行导航。这可能是我出现这种行为的原因吗?请参阅下面的答案,调用调度程序可能会解决您的问题。我在调度程序调用中导航是。否则我将得到一个运行时错误。这种情况发生的唯一时刻是在开始时。一旦该选项卡有了焦点,保持焦点或最小化窗口就无关紧要了。此外,导航完成时不会触发navigate_complete事件。所有这些都只发生在应用程序启动时。当你在启动时以编程方式聚焦所有选项卡时会发生什么?我曾用不同的方式尝试过几次。如何做到这一点?
tabItem2.Focus()应该可以做到这一点。调用
选项卡item.GotFocus
事件中的每个下一个焦点。我有很多选项卡,我会看看是否能找到一种方法来循环它们。
<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();
    }
}