【问题标题】:Geolocator.RequestAccessAsync throws "method called at unexpected time" exceptionGeolocator.RequestAccessAsync 抛出“在意外时间调用的方法”异常
【发布时间】:2017-06-01 06:26:25
【问题描述】:

我有一大段代码用于获取设备位置。这是在monogame windows uwp 项目中完成的。以下代码以前工作过(我在 VS2015 社区)。我最近做了一个新的操作系统并安装了 VS2017 社区。我终于得到了我的项目来构建和运行。唯一不起作用的是 Geolocator.RequestAccessAsync 正在引发“在意外时间调用的方法”异常。有什么想法吗?

public async Task<Vector2> GetDeviceGps()
{
        var accessStatus = await Geolocator.RequestAccessAsync();
        switch (accessStatus)
        {
            case GeolocationAccessStatus.Allowed:

                // If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
                Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 10 };

                // Subscribe to the StatusChanged event to get updates of location status changes.
                //geolocator.StatusChanged += OnStatusChanged;

                // Carry out the operation.
                Geoposition pos = await geolocator.GetGeopositionAsync();
                return new Vector2((float)pos.Coordinate.Point.Position.Longitude, (float)pos.Coordinate.Point.Position.Latitude);

            case GeolocationAccessStatus.Denied:
                //Do something!
                break;

            case GeolocationAccessStatus.Unspecified:
                //Murp
                break;
        }

        return Vector2.Zero;
    }

这是这样调用和处理的(从游戏的更新方法中调用):

mapper.GetDeviceGps().ContinueWith(pos =>
{
      Vector2 bob = pos.Result;

 });

【问题讨论】:

    标签: exception gps uwp monogame


    【解决方案1】:

    当您在应用中首次运行 RequestAccessAsync 时,系统会要求用户授予您的应用位置权限。因此该方法需要在 UI 线程中执行。

    【讨论】:

    • 这在技术上是答案,但没有回答“为什么这段代码以前有效但现在无效”的问题?我想也许在monogame 3.5和3.6之间。我没有看到任何证据。对于那些试图在 monogame 中进行地理定位的人,我必须在托管游戏的 XAML 页面中启动它(GamePage.xaml.cs - UI 线程)。这可能意味着访问状态的变化也可能必须在那里处理。不方便!
    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多