关于WindowsFormsHost中带有WebBrowser的c#:WPF应用-在加载网站时非常慢

WPF app with WebBrowser in WindowsFormsHost - very slow at loading website

我有一个WindowsFormsHost,在WPF应用程序中包含一个WebBrowser控件。 我的问题是,如果我第一次尝试加载网站,它的运行速度很慢。 我也尝试使用WPF WebBrowser,但是它有相同的问题。
这个问题在只有一个WebBrowser的小型示例应用程序中还是在大型应用程序中都是相同的,所以我的功能都没有影响加载时间。
在我经常使用的Google Chrome浏览器中,我没有这个问题。 我应该考虑设置WebBrowser的任何属性吗? 这与在WPF应用程序中使用WebBrowser有关吗? 任何建议表示赞赏。

谢谢。

简单的示例应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Window x:Class="WebBrowserIntoWPFConceptTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="468" Width="840" xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars">
<Grid x:Name="grid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="80*"/>
        <RowDefinition Height="20*"/>
    </Grid.RowDefinitions>

    <!--<WebBrowser Grid.Row="0"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                x:Name="wb"/>-->
    <Button Grid.Row="1"
            Grid.Column="0"
            Width="50"
            Height="50"
            VerticalAlignment="Bottom"
            Click="Button_Click"/>
    <Button Grid.Row="1"
            Grid.Column="1"
            Width="50"
            Height="50" Click="Button_Click_1" />
</Grid>

1
namespace WebBrowserIntoWPFConceptTest

{
公共局部类MainWindow:Window
{
私有WindowsFormsHost winFormHost;
System.Windows.Forms.WebBrowser wf_wb;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    public MainWindow()
    {
        InitializeComponent();
        wf_wb = new System.Windows.Forms.WebBrowser();
        winFormHost = new WindowsFormsHost();
        winFormHost.Child = wf_wb;
        Grid.SetColumn(winFormHost, 0);
        Grid.SetRow(winFormHost, 0);
        Grid.SetColumnSpan(winFormHost, 2);
        grid.Children.Add(winFormHost);
    }



    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Uri uri = new Uri("http://www.google.com");
        wf_wb.Navigate(uri);
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        HtmlDocument htmlDoc = (HtmlDocument)wf_wb.Document;
    }
}

}


WebBrowser是IE的包装器。 因此,如果您尝试使用IE且速度很慢,则WebBrowser也将变慢。 可能是某种病毒使它变慢了。 因此,请使用virusscanner扫描计算机。

我还没有听说过这样的问题。 但是我自己(在IE和mozilla中)遇到了浏览器问题,其中页面只是没有加载,而运行病毒扫描最终解决了该问题。

如果那没有帮助,请检查ouf CefSharp项目https://github.com/chillitom/CefSharp我会告诉您使用它,因为它更像是" WPF"。 它允许您使用WebBrowser进行所有操作。