我想编写一个小的 Silverlight 应用程序,其中有三个 DataGrid。每个 Datagrid 都使用异步方法从 Web 服务获取数据。现在,我希望第一个数据网格从 web 服务获取数据,而不是从 web 服务获取第二个数据网格的数据,其中参数来自第一个数据网格中选定行的参数,第三个数据网格具有前两个数据网格的参数。第一个数据网格通过注册事件处理程序获取 MainPage 方法中的数据,然后使用异步方法。
现在我的问题是我在 SelectionChanged(事件处理)方法中对其他数据网格使用异步方法,我猜这是错误的概念,因为在数据网格 2 中选择某些内容并返回到数据网格 1 后,所有数据网格都消失了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using Rebat.SymptomeService;
namespace Rebat
{
public partial class MainPage : UserControl
{
ServiceClient client = new ServiceClient();
public MainPage()
{
InitializeComponent();
ServiceClient client = new ServiceClient();
client.SymptomeListCompleted += new EventHandler<SymptomeListCompletedEventArgs>(client_SymptomeListCompleted);
client.SymptomeListAsync();
}
void client_SymptomeListCompleted(object sender, SymptomeListCompletedEventArgs e)
{
CustomerGrid.ItemsSource = e.Result;
}
void client_CustomerListCompleted(object sender, CustomerListCompletedEventArgs e)
{
CustomerGrid2.ItemsSource = e.Result;
}
void client_SalzListCompleted(object sender, SalzListCompletedEventArgs e)
{
SalzGrid.ItemsSource = e.Result;
}
private void CustomerGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome sympt = CustomerGrid.SelectedItem as Symptome;
client.CustomerListCompleted += new EventHandler<CustomerListCompletedEventArgs>(client_CustomerListCompleted);
client.CustomerListAsync(sympt.sId.ToString());
}
private void CustomerGrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome2 sympt2 = CustomerGrid2.SelectedItem as Symptome2;
client.SalzListCompleted += new EventHandler<SalzListCompletedEventArgs>(client_SalzListCompleted);
//this is the PROBLEM:
client.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());
}
}
}
我必须改变什么,或者我必须如何使用异步方法?您可以在事件处理方法中使用异步方法吗?这是否适用于使用网络服务?