Silverlight 4异步等待怎么样?

时间:2011-08-28 22:45:55

标签: silverlight asynchronous

我遇到了问题:

//Get All master record
entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();

 //Here I am calling my web service to get data
 QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);

 //Loop to show questions
 int iNumber=1;
 foreach (var oIn in entryE_QuestMaster)
   {
      Node subNode = new Node();
      subNode.Content = oIn.e_Question;
      subNode.Name = "Quest_" + iNumber.ToString().Trim();
      subNode.Tag = oIn.e_QID.ToString();
      subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
      iNumber++;
      this.tvMainNode.Nodes.Add(subNode);
    }

每当我打电话

QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);

它运行以下代码

 public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData)
        {
            ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
            eLearningDataServiceClient client = new eLearningDataServiceClient();
            isSync = true;
            client.getExamsMasterCompleted+=(s,e)=>
                {
                    iCollectionDataResult = e.Result;
                };
            client.getExamsMasterAsync(ID);

        }

我的问题是我想等到我的e.result回到iCollectionDataResult

目前,在调用此服务后,系统将继续执行foreach循环中的下一行代码。在这个阶段,我的entryE_QuestMaster没有任何记录,我只是想等到循环继续之前我的结果回来。

回答ChrisF后

没有想到chrisf所说的对我有用,但是我想在我的MVVM课程中做所有事情,而不是在表单级别,这里我的代码有所改变,我仍然需要你的帮助,并且想要做些什么专业代码,而不只是编写巨大的代码。

我在我的MVVM类中添加了这两行

        public delegate void ShowQuestionTreeView(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster);
    public event ShowQuestionTreeView ShowQuestionforTreeview;

然后在方法中我添加了这个

  /// <summary>
    /// 
    /// </summary>
    /// <param name="ID"></param>
    public void getExamsMasterbyExamID(int ID, ref ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionData)
    {
        ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionDataResult = iCollectionData;
        eLearningDataServiceClient client = new eLearningDataServiceClient();
        client.getExamsMasterbyExamIDCompleted+= (s, e) =>
            {
                iCollectionDataResult = e.Result;
                **ShowQuestionforTreeview(iCollectionDataResult);**
            };
        client.getExamsMasterbyExamIDAsync(ID);
    }

在客户端我已经完成了这个

            //Generate Treeview for question
        QuestVM.ShowQuestionforTreeview += new eQuestCreateVM.ShowQuestionTreeView(QuestVM_ShowQuestionforTreeview);

方法:

  void QuestVM_ShowQuestionforTreeview(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster)
    {
        //Loop to show questions
        int iNumber = 1;
        foreach (var oIn in iResultQuestMaster)
        {
            Node subNode = new Node();
            subNode.Content = oIn.e_Question;
            subNode.Name = "Quest_" + iNumber.ToString().Trim();
            subNode.Tag = oIn.e_QID.ToString();
            subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
            subNode.Title = oIn.e_Question_Text;
            iNumber++;
            tvCreateQuestion.Nodes[0].Nodes.Add(subNode);              
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要移动此代码:

       client.getExamsMasterCompleted+=(s,e)=>
            {
                iCollectionDataResult = e.Result;
            };

在您致电getExamsMasterbyExamID之外。这也意味着移动您声明并初始化eLearningDataServicesClient的位置。

此时设置代码的方式意味着您假设在方法返回时将设置iCollectionDataResult。然而,这种情况并非如此。该方法将在getExamsMasterCompleted事件被触发之前返回。

您需要按以下方式组织代码:

eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterCompleted += (s,e) =>
{
    //Loop to show questions
}

client.getExamsMasterAsync(ID);

或者将问题的显示绑定到ObservableCollection,以便在设置时自动更新UI。