How to make the data from my WCF Service to get stored inside my Silverlight application?
I have Silverlight application that retrieves data from the database through a WCF Service. What my application have to do is to display the referent da开发者_如何学Pythonta anytime a button gets MouseOvered. I did it in a way that that when a button gets MouseOvered, I called my service and retrieved the data, but it generetad a big delay. Now, I think that another way should be making a list of all objects from the table, and just searching the id in the list when the action is triggered. I started coding, but it resulted in fail (and such an ugly piece of code).
My Working Code
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;
}
What I tried to do
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);
}
}
Maybe I'm missing something really big, but my webService_GetDataCompleted
method is never reached.
Thanks in advance folks!
Is this call:
webService.GetCounterAsync();
used to set counter
?
If so you're not letting it finish before looping with counter
as the termination value. It's highly likely that counter
is still 0
so your GetDataAsync
calls aren't happening.
for (int i = 0; i < counter; i++)
{
webService.GetDataAsync(i);
}
Move this code into your webService_GetCounterCompleted
method, so your code becomes:
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);
}
}
Plus your existing webService_GetDataCompleted
method.
精彩评论