跟踪位置时,Windows Phone 8的System.Reflection.TargetInvocationException

时间:2014-10-22 22:18:56

标签: c# windows-phone-8 location system.reflection

我正在开发基于GPS服务的应用程序,我必须连续跟踪用户的位置,例如HERE地图,并且我使用下面的代码:

geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 20; //Doesn't matter the value I put here, it won't work
geolocator.PositionChanged += geolocator_PositionChanged;

void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    Dispatcher.BeginInvoke(() =>
    {
        if(args.Position != null)
        {
             myPosition = args.Position.Coordinate.ToGeoCoordinate();
             UpDateData();
        }
     });
}

问题是:我收到了System.Reflection.TargetInvocationException

你对这类问题有什么解决方法吗?

3 个答案:

答案 0 :(得分:0)

此错误很可能是因位置未在应用程序清单文件中将标记为

答案 1 :(得分:0)

正如@Stuart提到的那样,请确保您已从ID_CAP_LOCATION文件中勾选AppManifest。如果您不这样做,您的应用程序将抛出异​​常,当您尝试在开发期间部署它时将导致您的应用程序失败。

How to continuously track the phone's location for Windows Phone 8

答案 2 :(得分:0)

        try
        {
            geolocator = new Geolocator();
            geolocator.DesiredAccuracy = PositionAccuracy.High;
            geolocator.ReportInterval = 2000;
            geolocator.PositionChanged += geolocator_PositionChanged;
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Location  is Disabled in Phone Settings.");
        }

        private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
        try
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (args.Position != null && args.Position.Coordinate.ToGeoCoordinate() != myPosition)
                {
                    if(args.Position.Coordinate.Accuracy <= 1500)
                    {
                        myPosition = args.Position.Coordinate.ToGeoCoordinate();
                        UpDateMyPositionCircle(args.Position.Coordinate.Accuracy);
                    }
                }
            });
        } 
        catch (TargetInvocationException tie) 
        {
            if (tie.Data == null) throw;
            else MessageBox.Show("TargetInvocationException while Tracking: " + tie.InnerException.ToString());                    
        }
        catch(SystemException se)
        {
            if (se.Data == null) throw;
            else MessageBox.Show("SystemException while Tracking: " + se.InnerException.ToString());
        }
        catch(Exception ex)
        {
            if (ex.Data == null) throw;
            else MessageBox.Show("Exception while Tracking: " + ex.InnerException.ToString());
        }
    }