当前位置: 首页 > 编程语言 > C#

如何等待QueryCompleted事件?分享

时间:2023-04-10 23:39:13 C#

如何等待QueryCompleted事件?我创建了一个小型测试应用程序来获取经度和纬度并将它们转换为实际地址:usingSystem;使用System.Collections.Generic;使用System.Collections.ObjectModel;使用系统诊断;使用System.Device.Location;使用System.Linq;使用System.Net;使用系统线程;使用System.Threading.Tasks;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Navigation;.Phone.Maps.Services;使用Microsoft.Phone.Shell;使用PhoneApp1.Resources;使用Windows.Devices.Geolocation;namespacePhoneApp1{publicpartialclassMainPage:PhoneApplicationPage{privateGeoCoordinateLocation;公共ObservableCollection地址{得到;放;}//构造函数publicMainPage(){InitializeComponent();//本地化ApplicationBar的示例代码//BuildLocalizedApplicationBar();}protectedoverrideasyncvoidOnNavigatedTo(NavigationEventArgse){awaitGetLocation();}publicasyncTaskGetLocation(){Location=awaitCoordinateConverter.GetLocation();ReverseGeoCoding.StartReverseGeoCoding(位置);//ReverseGeoCoding.done.WaitOne();字符串地址=ReverseGeoCoding.Address;}}publicstaticclassReverseGeoCoding{publicstaticObservableCollectionAddresses=newObservableCollection();公共静态字符串地址;公共静态布尔已完成;publicstaticAutoResetEventdone=newAutoResetEvent(true);publicstaticvoidStartReverseGeoCoding(GeoCoordinateLocation){Completed=false;varreverseGeocode=newReverseGeocodeQuery();reverseGeocode.GeoCoordinate=newGeoCoordinate(Location.Latitude,Location.Longitude);reverseGeocode.QueryCompleted+=ReverseGeocodeQueryCompleted;done.Reset();reverseGeocode.QueryAsync();}publicstaticvoidReverseGeocodeQueryCompleted(objectsender,QueryCompletedEventArgse){varreverseGeocode=senderas反向地理编码查询;如果(reverseGeocode!=null){reverseGeocode.QueryCompleted-=ReverseGeocodeQueryCompleted;}//Microsoft.Phone.Maps.Services.MapAddress地址;地址.清除();if(!e.Cancelled){foreach(varaddressine.Result.Select(adrInfo=>adrInfo.Information.Address)){Addresses.Add(string.Format("{0}{1},{2}{3}{4},{5}",address.HouseNumber,address.Street,address.City,address.State,address.PostalCode,address.Country).Trim());}}if(Addresses.Count>0){Address=Addresses[0].ToString();}else{地址="";}done.Set();完成=真;}}publicstaticclassCoordinateConverter{publicstaticGeoCoordinateConvertGeocoordinate(Geocoordinategeocoordinate){returnnewGeoCoordinate(geocoordinate.Latitude,geocoordinate.Longitude,geocoordinate.Altitude??Double.NaN,geocoordinate.Accuracy,geocoordinate.AltitudeAccuracy??Double.NaN,geocoordinate.Speed??Double.NaN,地理坐标标题??双.NaN);}publicstaticasyncTaskGetLocation(){//获取当前位置。地理定位器myGeolocator=newGeolocator();myGeolocator.DesiredAccuracy=PositionAccuracy.High;//myGeolocator.DesiredAccuracyInMeters=50;地理坐标myGeocoordinate=null;尝试{GeopositionmyGeoposition=awaitmyGeolocator.GetGeopositionAsync(maximumAge:TimeSpan.FromMinutes(1),timeout:TimeSpan.FromSeconds(10));myGeocoordinate=myGeoposition.Coordinate;}catch(Exceptionex){if((uint)ex.HResult==0x80004004){//应用程序没有正确的能力或位置主开关关闭MessageBox.Show("locationisdisabledinphonesettings");}}if(myGeocoordinate==null){返回GeoCoordinate.Unknown;}GeoCoordinatemyGeoCoordinate=CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);返回我的地理坐标;}}}代码工作正常,即调使用ReverseGeocodeQueryCompleted并正确计算地址但是,ReverseGeocodeQueryCompleted发生在GetLocation()完成之后,并且分配给Address的地址为空。我的问题是如何制作ReverseGeoCoding.StartReverseGeoCoding(Location);等待完成:ReverseGeocodeQueryCompleted(objectsender,QueryCompletedEventArgse){....}我尝试使用AutoResetEvent和WaitOne,但整个线程停止,代码永远不会到达ReverseGeocodeQueryCompleted()。我愿意接受有关如何解决此问题的建议。EitanB这是一个可以等待QuerryAsync的扩展方法:事件处理程序>>queryCompleted=null;queryCompleted=(send,arg)=>{//注销事件以便可以在同一对象上多次调用QueryTaskAsyncreverseGeocode.QueryCompleted-=queryCompleted;if(arg.Error!=null){tcs.SetException(arg.Error);}elseif(arg.Cancelled){tcs.SetCanceled();}else{tcs.SetResult(arg.Result);}};reverseGeocode.QueryCompleted+=queryCompleted;reverseGeocode.QueryAsync();返回tcs.Task;我稍微修改了Benoit的答案,使其看起来像这样:reverseGeocode.GeoCoordinate=newSystem.Device.Location.GeoCoordinate(Location.Latitude,Location.Longitudecs=t);新变种TaskCompletionSource();事件处理器>>h安德勒=空;handler=(sender,args)=>{if(args.Error!=null){tcs.SetException(args.Error);}elseif(args.Cancelled){tcs.SetCanceled();}else{Addresses.Clear();foreach(args.Result.Select(adrInfo=>adrInfo.Information.Address)中的var地址){Addresses.Add(string.Format("{0}{1},{2}{3}{4},{5}",address.HouseNumber,address.Street,address.City,address.State,address.PostalCode,address.Country).Trim());}stringAddress=Addresses.Count>0?地址=地址[0].ToString():string.Empty;reverseGeocode.QueryCompleted-=处理程序;tcs.SetResult(地址);}};reverseGeocode.QueryCompleted+=处理程序;reverseGeocode.QueryAsync();返回tcs.Task;}这将取下我的代码中的以下两个函数:reverseGeocode.GeoCoordinate=newGeoCoordinate(Location.Latitude,Location.Longitude);反向地理编码.QueryCompleted+=ReverseGeocodeQueryCompleted;reverseGeocode.QueryAsync();}publicstaticvoidReverseGeocodeQueryCompleted(objectsender,QueryCompletedEventArgs>e){varreverseGeocode=senderasReverseGeocodeQuery;如果(reverseGeocode!=null){reverseGeocode.QueryCompleted-=ReverseGeocodeQueryCompleted;}//Microsoft.Phone.Maps.Services.MapAddress地址;地址.清除();if(!e.Cancelled){foreach(varaddressine.Result.Select(adrInfo=>adrInfo.Information.Address)){Addresses.Add(string.Format("{0}{1},{2}{3}{4},{5}",address.HouseNumber,address.Street,address.City,address.State,address.PostalCode,address.Country.Trim());}}地址=(Addresses.Count>0)?地址[0].ToString():string.Empty;如果一切正常,再次感谢Benoit!任务同步自己查TaskCompletionSource后面会写更好的回复。同时,查看诺基亚开发者Wiki上的异常/等待联系服务。以上就是C#学习教程:如何等待QueryCompleted事件?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: