0

我正在开发 Windows Phone 8 项目。在我的项目中,有 10 个事件和 10 个事件处理程序 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();

        }

它卡在第一个项目上。在里面并等待......等待......无法传递给下一个元素。

4

1 回答 1

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

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

于 2013-06-03T20:24:24.720 回答