我有Silverlight应用程序,它通过WCF服务从数据库中检索数据。我的应用程序必须做的是在按钮获得MouseOvered时随时显示引用数据。我这样做的方式是,当一个按钮获得MouseOvered时,我调用了我的服务并检索了数据,但它确实有很大的延迟。现在,我认为另一种方法应该是从表中列出所有对象,并在触发操作时只搜索列表中的id。我开始编码,但它导致失败(以及如此丑陋的代码片段)。
我的工作代码
private void MouseOverHarbor(object sender, RoutedEventArgs e)
{
Ellipse thisPath = (Ellipse)sender;
DataRetrieverReference.DataRetrieverClient webService = new DataRetrieverReference.DataRetrieverClient();
webService.GetDataCompleted += new EventHandler<DataRetrieverReference.GetDataCompletedEventArgs>(webService_GetDataCompleted);
webService.GetDataAsync(Convert.ToInt32(thisPath.DataContext));
}
void webService_GetDataCompleted(object sender, WebPortos.DataRetrieverReference.GetDataCompletedEventArgs e)
{
NameField.Text = e.Result.Name;
CityField.Text = e.Result.City;
StateField.Text = e.Result.State;
CompanyField.Text = e.Result.Company;
}
我尝试做什么
private List<vwPortos_SEP> harborList;
private int counter;
public Brasil()
{
InitializeComponent();
this.harborList = new List<vwPortos_SEP>();
DataRetrieverClient webService = new DataRetrieverClient();
webService.GetCounterCompleted += new EventHandler<GetCounterCompletedEventArgs>(webService_GetCounterCompleted);
webService.GetCounterAsync();
webService.GetDataCompleted += new EventHandler<DataRetrieverReference.GetDataCompletedEventArgs>(webService_GetDataCompleted);
for (int i = 0; i < counter; i++)
{
webService.GetDataAsync(i);
}
}
void webService_GetDataCompleted(object sender, WebPortos.DataRetrieverReference.GetDataCompletedEventArgs e)
{
MessageBox.Show("It works!");//It doesn't work!
try
{
this.harborList.Add(e.Result);
}
catch (Exception exc)//It doesn't even throw ecxpetions, this method is never reached.
{
MessageBox.Show(exc.Message);
MessageBox.Show(exc.InnerException.Message);
}
}
也许我错过了一些非常重要的东西,但我的webService_GetDataCompleted
方法永远不会到达。
先谢谢大家!
答案 0 :(得分:1)
这是电话:
webService.GetCounterAsync();
用于设置counter
?
如果是这样,那么在循环使用counter
作为终止值之前,你不会让它完成。 counter
很可能仍然是0
,因此您的GetDataAsync
来电不会发生。
for (int i = 0; i < counter; i++)
{
webService.GetDataAsync(i);
}
将此代码移至webService_GetCounterCompleted
方法,以便您的代码变为:
public Brasil()
{
InitializeComponent();
this.harborList = new List<vwPortos_SEP>();
DataRetrieverClient webService = new DataRetrieverClient();
webService.GetCounterCompleted +=
new EventHandler<GetCounterCompletedEventArgs>(webService_GetCounterCompleted);
webService.GetCounterAsync();
}
void webService_GetCounterCompleted(object sender,
WebPortos.DataRetrieverReference.GetCounterCompletedEventArgs e)
{
webService.GetDataCompleted +=
new EventHandler<DataRetrieverReference.GetDataCompletedEventArgs>(webService_GetDataCompleted);
for (int i = 0; i < counter; i++)
{
webService.GetDataAsync(i);
}
}
加上您现有的webService_GetDataCompleted
方法。