C# 从列表中导航WebBrowser

C# 从列表中导航WebBrowser,c#,wpf,xaml,C#,Wpf,Xaml,当我启动此程序时,我只会进入MessageBox,但是 我应该得到,然后。那么,问题出在哪里?那么,你能帮我解决这个问题吗?谢谢:) 有一个XAML代码: <Window x:Class="WpfApplication19.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/

当我启动此程序时,我只会进入MessageBox,但是 我应该得到,然后。那么,问题出在哪里?那么,你能帮我解决这个问题吗?谢谢:)

有一个XAML代码:

<Window x:Class="WpfApplication19.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="800" Width="800">
<Grid Name="grid">
    <Button Name="btn" Content="Navigate" HorizontalAlignment="Left" Margin="697,223,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    <Button Name="shower" Content="getValues" HorizontalAlignment="Left" Margin="697,262,0,0" VerticalAlignment="Top" Width="75" Click="shower_Click"/>
</Grid></Window>

还有一个C#短代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using mshtml;

namespace WpfApplication19
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<string> list = new List<string>();
        private List<string> arrOfAdresses = new List<string>();
        private WebBrowser thisIsWeb;
        public MainWindow()
        {
            InitializeComponent();
            list.Add("http://www.yahoo.com");
            list.Add("http://www.google.com");
            thisIsWeb = new WebBrowser();
            thisIsWeb.Width = thisIsWeb.Height = 400;
            this.grid.Children.Add(thisIsWeb);
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            foreach (string s in list)
            {
                thisIsWeb.Navigate(s);
                thisIsWeb.LoadCompleted += OnthisIsWebCompleted;
            }
        }

        private void OnthisIsWebCompleted(object sender, NavigationEventArgs e)
        {
            WebBrowser bro = sender as WebBrowser;
            this.arrOfAdresses.Add(bro.Source.ToString());
        }

        private void shower_Click(object sender, RoutedEventArgs e)
        {
            foreach (string s in arrOfAdresses)
                MessageBox.Show(s);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用mshtml;
命名空间WpfApplication19
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私有列表=新列表();
私有列表arrOfAdresses=新列表();
私人网络浏览器thisIsWeb;
公共主窗口()
{
初始化组件();
列表。添加(“http://www.yahoo.com");
列表。添加(“http://www.google.com");
thisIsWeb=新的WebBrowser();
thisIsWeb.宽度=thisIsWeb.高度=400;
this.grid.Children.Add(thisIsWeb);
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
foreach(列表中的字符串s)
{
这是web.Navigate;
这是web.LoadCompleted+=已完成的日期;
}
}
已完成(对象发送方、NavigationEventArgs e)上的私有无效
{
WebBrowser bro=发送方作为WebBrowser;
this.arrOfAdresses.Add(bro.Source.ToString());
}
私有无效对象(对象发送器,路由目标e)
{
foreach(arrOfAdresses中的字符串s)
MessageBox.Show(s);
}
}
}
非常简单:

您正在使用
LoadCompleted
填充
arrofades
。调用navigate不能保证调用
LoadCompleted
<代码>加载完成会在您导航到的页面加载后立即调用(顾名思义)

由于您紧接着导航到两个不同的URL,因此只有最后一次调用
navigate
会导致加载网站-下一次调用
navigate
会隐式“中止”先前的导航


因此,唯一成功加载(并因此引发
LoadCompleted
)的网站是
http://www.google.com

嗯,很有趣。有什么方法可以让我展示谷歌网站和雅虎网站吗?你想要的确切行为是什么?等待google被加载,然后导航到yahoo(或者更确切地说,像在你的列表中那样的其他方式)?在第一个站点加载后自动导航到另一个站点的目的是什么我想在雅虎上导航,保存地址,和谷歌做同样的事情。目的是,我想在页面上导航,从中获取数据(例如,通过mshtml获取某人的姓名)。当我获取数据时,我想通过MessageBox显示。哦,好吧,尽管我不确定我的目的是否正确:你想从每个页面提取数据吗?哦,也许你想看看,比如说,有人现在通过浏览谷歌网站登录了吗?是的,或者我访问了Facebook的个人资料,在那里我想获得照片的数量,然后我应该显示姓名和照片的数量。但问题在清单中。例如,我想有10个配置文件,我想加载每个配置文件。