1

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

谢谢。

简单的示例应用程序:

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

namespace WebBrowserIntoWPFConceptTest

{公共部分类MainWindow:窗口{私有WindowsFormsHost winFormHost;System.Windows.Forms.WebBrowser wf_wb;

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

}

4

1 回答 1

0

WebBrowser 是 IE 的包装器。因此,如果您尝试 IE 并且它很慢,那么 WebBrowser 也会很慢。可能是某种病毒使它变慢了。所以用virusscanner扫描你的电脑。

我没有听说过这样的问题。但是,我自己(在 IE 和 mozilla 中)遇到了浏览器问题,其中页面没有加载并且运行病毒扫描最终修复了它。

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

于 2012-12-30T22:31:51.287 回答