2013-06-03 22 views
0

我正在開發Windows Phone 8項目。在我的項目中,有10個EventHandler的事件ReverseGeocodeQuery_QueryCompleted(1到10)。當第一個EventHandler完成時,它打開第二個事件。如何處理多個EventHandlers到AsyncQuery

我應該實現什麼來管理那些沒有太多代碼的事件。

代碼
myReverseGeocodeQuery = new ReverseGeocodeQuery(); 
myReverseGeocodeQuery.GeoCoordinate = mySimulationCoordinates.ElementAt(0); 
myReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_1; 
myReverseGeocodeQuery.QueryAsync(); 

private void ReverseGeocodeQuery_QueryCompleted_1(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) 
     { 
      if (e.Error == null) 
      { 
       if (e.Result.Count > 0) 
       { 
        MapAddress address = e.Result[0].Information.Address; 
        label8txt.Text = address.City.ToString() + "\n" + address.Street.ToString(); 
        StringBuilder str = new StringBuilder(); 
        str.AppendLine("Pierwszy"); 
        str.AppendLine("11" + address.HouseNumber); 
        str.AppendLine("17" + address.Street); 
        MessageBox.Show(str.ToString()); 

       } 
       myReverseGeocodeQuery = new ReverseGeocodeQuery(); 
       myReverseGeocodeQuery.GeoCoordinate = mySimulationCoordinates.ElementAt(1); 
       myReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_2; 
       myReverseGeocodeQuery.QueryAsync(); 
      } 
     } 
     private void ReverseGeocodeQuery_QueryCompleted_2(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) 
     { 
      if (e.Error == null) 
      { 
       if (e.Result.Count > 0) 
       { 
        MapAddress address = e.Result[0].Information.Address; 
        label8txt.Text = address.City.ToString() + "\n" + address.Street.ToString(); 
        StringBuilder str = new StringBuilder(); 
        str.AppendLine("Drugi"); 
        str.AppendLine("11" + address.HouseNumber); 
        str.AppendLine("17" + address.Street); 
        MessageBox.Show(str.ToString()); 

        myReverseGeocodeQuery = new ReverseGeocodeQuery(); 
        myReverseGeocodeQuery.GeoCoordinate = mySimulationCoordinates.ElementAt(2); 
        myReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_3; 
        myReverseGeocodeQuery.QueryAsync(); 

       } 
      } 
     } 

實施例解決方案1 ​​

public class DataContainer 
    { 
     public string Description { get; set; } 
     public GeoCoordinate Coordinate { get; set; } 
     //public List<GeoCoordinate> mySimulationCoordinates { get; set; } 

     public String EnterSimulation() { 

      StringBuilder strRet = new StringBuilder(); 
      List<GeoCoordinate> mySimulationCoordinates = new List<GeoCoordinate>(); 
      mySimulationCoordinates.Add(new GeoCoordinate(51.760752, 19.458216)); 
      mySimulationCoordinates.Add(new GeoCoordinate(51.760757, 19.458356)); 
      mySimulationCoordinates.Add(new GeoCoordinate(51.760738, 19.458442)); 
      mySimulationCoordinates.Add(new GeoCoordinate(51.7607, 19.458501)); 
      mySimulationCoordinates.Add(new GeoCoordinate(51.760662, 19.458533)); 

      var descriptions = new[] { "Pierwszy", "Drugi", "Trzeci", "Czwarty", "Piąty" }; //etc 
      var zipped = mySimulationCoordinates.Zip(descriptions, (coord, desc) => new DataContainer { Description = desc, Coordinate = coord }); 

      int k = zipped.Count(); 


      foreach (var item in zipped) 
      { 
       var currentItem = item; 
       using (var waitHandle = new AutoResetEvent(false)) 
       { 
        var geocodeQuery = new ReverseGeocodeQuery(); 
        geocodeQuery.GeoCoordinate = item.Coordinate; 
        geocodeQuery.QueryCompleted += (sender, args) => 
        { 
         if (args.Error == null) 
         { 
          if (args.Result.Count > 0) 
          { 
           MapAddress address = args.Result[0].Information.Address; 
           //label8txt.Text = address.City.ToString() + "\n" + address.Street.ToString(); 
           StringBuilder str = new StringBuilder(); 
           str.AppendLine(currentItem.Description); 
           str.AppendLine("House Number" + address.HouseNumber); 
           str.AppendLine("Street " + address.Street); 
           strRet.AppendLine("->"); 
           strRet.Append(str); 
           waitHandle.Set(); 
          } 
         } 
        }; 

        geocodeQuery.QueryAsync(); 
        waitHandle.WaitOne(); 
       } 

     } 
      return strRet.ToString(); 

     } 

它粘在第一項。在裏面,等待......等等......不能傳遞給下一個元素。

回答

1

呃...讓我們來看看,這難道不是更容易嗎?

警告:未經測試

public class DataContainer 
{ 
    public string Description {get;set;} 
    public GeoCoordinate Coordinate {get;set;} 
} 
var descriptions = new[] {"Pierwszy" , "Drugi" , "Trzeci" }; //etc 
var zipped = mySimulationCoordinates.Zip(descriptions, (coord, desc) => new DataContainer { Description = desc, Coordinate = coord }); 

foreach(var item in zipped) 
{ 
    var currentItem = item; 
    using(var waitHandle = new AutoResetEvent(false)) 
    { 
     var geocodeQuery = new ReverseGeocodeQuery(); 
     geocodeQuery.GeoCoordinate = currentItem.Coordinates; 
     geocodeQuery.QueryCompleted += (sender, args) => { 
      if (e.Error == null) 
      { 
       if (e.Result.Count > 0) 
       { 
        MapAddress address = args.Result[0].Information.Address; 
        label8txt.Text = address.City.ToString() + "\n" + address.Street.ToString(); 
        StringBuilder str = new StringBuilder(); 
        str.AppendLine(currentItem.Description); 
        str.AppendLine("11" + address.HouseNumber); 
        str.AppendLine("17" + address.Street); 
        MessageBox.Show(str.ToString()); 
        waitHandle.Set(); 
       } 
      } 
     }; 

     geoCodeQuery.QueryAsync(); 
     waitHandle.WaitOne(); 
    } 
} 

這應該向你保證,一個事件是爲了接連處理。

+0

有趣...測試進行中:) – boski

+0

它不能通過第1項。我用你的代碼更新了我的問題。 – boski