InnerException = {“来自HRESULT的异常:0x8004231C”}

时间:2016-03-04 13:06:29

标签: c# silverlight windows-phone-8.1

我正在Windows Phone Silverlight 8.1中开发一个应用程序,我试图使用this链接设置当前位置和硬编码目的地之间的路由,代码为:

private async void GetCoordinates()
        {
            Geolocator myGeolocator = new Geolocator();
            myGeolocator.DesiredAccuracyInMeters = 50;
            Geoposition myposition = null;
            try
            {
                myposition = await myGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1),TimeSpan.FromSeconds(50));
                Geocoordinate myGeocoordinate = myposition.Coordinate;
                GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
                mycoordinates.Add(new GeoCoordinate(myGeoCoordinate.Latitude, myGeoCoordinate.Longitude));
                locationmap.Center = myGeoCoordinate;
                locationmap.ZoomLevel = 13;

                Mygeocodequery = new GeocodeQuery();
                Mygeocodequery.SearchTerm = "Seattle, WA";
                Mygeocodequery.GeoCoordinate = new GeoCoordinate(myGeoCoordinate.Latitude, myGeoCoordinate.Longitude);
                Mygeocodequery.QueryCompleted += Mygeocodequery_Querycompleted;
                Mygeocodequery.QueryAsync();
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Mygeocodequery_Querycompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
        {
            if(e.Error==null)
            {
                MyQuery = new RouteQuery();
                mycoordinates.Add(e.Result[0].GeoCoordinate);
                MyQuery.Waypoints = mycoordinates;
                MyQuery.QueryCompleted += MyQuery_QueryCompleted;
                MyQuery.QueryAsync();
                Mygeocodequery.Dispose();
            }
        }

        private void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
        {
            try
            {
                    **Route MyRoute = e.Result;**
                    MapRoute MyMapRoute = new MapRoute(MyRoute);
                    locationmap.AddRoute(MyMapRoute);
                    MyQuery.Dispose();
            }
            catch (TargetInvocationException ex)
            {
                MessageBox.Show(ex.InnerException.Message);
            }
        }

它有时工作正常但有时我得到这个内部异常作为TargetInvocationException Exception from HRESULT: 0x8004231C

在突出显示的行上,请告诉我该怎么办?

1 个答案:

答案 0 :(得分:0)

问题仅仅是因为我跳过了在地图控件的加载事件中编写以下代码的步骤

private void locationmap_Loaded(object sender, RoutedEventArgs e)
    {
        Microsoft.Phone.Maps.MapsSettings.ApplicationContext.ApplicationId = "yourapplicationID";
        Microsoft.Phone.Maps.MapsSettings.ApplicationContext.AuthenticationToken = "yourauthenticationtoken";
    }

如果添加此代码,它适用于所有位置。