如何处理AsyncQuery的多个EventHandler

时间:2013-06-03 19:45:00

标签: c# windows-phone-7 events event-handling windows-phone-8

我正在开发Windows Phone 8项目。在我的项目中有10个EventHandlers ReverseGeocodeQuery_QueryCompleted(1到10)。第一个EventHandler完成后,它会打开第二个事件。

如果没有这么多代码,我应该实现什么来管理这些事件。

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 个答案:

答案 0 :(得分: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();
    }
}

这应该保证您按顺序处理一个事件。