【问题标题】:System.Reflection.TargetInvocationException with Windows phone 8 when tracking location跟踪位置时 Windows phone 8 的 System.Reflection.TargetInvocationException
【发布时间】:2014-10-22 22:18:56
【问题描述】:

我正在开发基于 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

你有解决这类问题的办法吗?

【问题讨论】:

  • 确保 ID_CAP_LOCATION 在 Capabilities 中被选中。如果这不能解决您的问题,我会假设它来自 UpDateData(),请将其注释掉。
  • ID_CAP_LOCATION 是我开始开发部分时检查的第一件事
  • 在 UpDateData() 函数中,我只根据我当前的位置将图钉放在地图控件中
  • 如果你把它注释掉会发生什么?

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


【解决方案1】:
        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());
        }
    }

【讨论】:

    【解决方案2】:

    这个错误很可能是由于 location 在您的应用程序清单文件中没有被标记为 on

    【讨论】:

    • ID_CAP_LOCATION 是我开始开发部分时检查的第一件事
    【解决方案3】:

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

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

    【讨论】:

    • ID_CAP_LOCATION 是我开始开发部分时检查的第一件事
    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多