如何处理多个eventandler到AsyncQuery



我正在做Windows Phone 8项目。在我的项目中有10个事件与10个eventandlers ReverseGeocodeQuery_QueryCompleted(1到10)。当第一个eventandler完成时,它会开启第二个事件。

我应该实现管理这些事件没有这么多的代码。

<标题> 代码
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();
        }

卡在第一项上。在里面等着……等待……不能传递给下一个元素。

嗯…我想想,这样不是更简单吗?

警告:未经考验的

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();
    }
}

相关内容

  • 没有找到相关文章

最新更新