首先,我发布我的代码:
public partial class MainWindow : Window
{
private readonly BackgroundWorker worker = new BackgroundWorker();
private List<string> list = new List<string>();
private List<string> arrOfAdresses = new List<string>();
public MainWindow()
{
InitializeComponent();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
list.Add("http://www.yahoo.com");
list.Add("http://www.google.com");
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("All is done");
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
foreach (string s in list)
{
WebBrowser bro = new WebBrowser();
bro.Width = bro.Height = 1;
grid.Children.Add(bro);
bro.Navigate(s);
bro.LoadCompleted += OnLoadCompleted;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
worker.RunWorkerAsync();
}
private void OnLoadCompleted(object sender, NavigationEventArgs e)
{
WebBrowser bro = sender as WebBrowser;
this.arrOfAdresses.Add(bro.Source.ToString()+"Added text");
MessageBox.Show("xxx"); //MessageBox is ignored
}
private void shower_Click(object sender, RoutedEventArgs e)
{
arrOfAdresses.Reverse();
foreach (string s in arrOfAdresses)
MessageBox.Show(s);
}
}
button_click
event 应该将地址存储到arrOfAdresse
中,这将稍作修改。
接下来,shower_Click
应该显示所有修改过的值arrOfAdresses
。当我想显示我应该拥有的值时arrOfAdresses
,它什么也没给我。我认为,问题出在LoadCompleted
事件中,因为当我放置 MessageBox 时,程序会忽略它。有什么方法可以显示价值,什么时候shower_Click
提出,有什么方法可以解决它?感谢您的回复。