如何使我的WCF服务中的数据存储在我的Silverlight应用程序中?

时间:2010-09-03 19:28:20

标签: c# silverlight wcf

我有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方法永远不会到达。

先谢谢大家!

1 个答案:

答案 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方法。