我有以下功能:
private async void SearchLocation(string location)
{
var myPosition = await new Geolocator().GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
// Define search
var geoQuery = new GeocodeQuery();
geoQuery.SearchTerm = location;
geoQuery.GeoCoordinate = new GeoCoordinate(myPosition.Coordinate.Latitude, myPosition.Coordinate.Longitude);
geoQuery.QueryCompleted += (s, f) =>
{
if (f.Error == null && f.Result.Count > 0)
{
map.Center = f.Result[0].GeoCoordinate;
map.ZoomLevel = 12;
}
};
geoQuery.QueryAsync();
}
它正在搜索位置并在地图上显示。这很好,但是当我点击地图时,我的应用程序崩溃了(map_Tap为空)。
在输出中出现:
类型' System.InvalidOperationException'的第一次机会异常。发生在System.Windows.ni.dll
中
堆栈追踪:
> Notifier.DLL!gpsNotifier.App.Application_UnhandledException(object sender, System.Windows.ApplicationUnhandledExceptionEventArgs e) Line 112 C#
System.Windows.ni.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e) Unknown
System.Windows.ni.dll!MS.Internal.Error.IsNonRecoverableUserException(System.Exception ex, out uint xresultValue) Unknown
System.Windows.ni.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj, System.IntPtr unmanagedObjArgs, int argsTypeIndex, int actualArgsTypeIndex, string eventName) Unknown
我什么都不懂。 Visual Studio中的异常很难分析,因为它们不会显示错误发生的位置。我的功能有什么问题?我通过使用SearchLocation(" Warsaw")在按钮中执行它;
编辑:
在SearchLocatio之前,我正在调用另一个函数:
private async void ShowMyLocationOnTheMap()
{
Geolocator myGeolocator = new Geolocator();
Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
GeoCoordinate myGeoCoordinate =
CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
this.map.Center = myGeoCoordinate;
this.map.ZoomLevel = 15;
currentLocation.coordinates = myGeoCoordinate;
}