2017-06-01 148 views
0

我有一大块代码用于获取设备位置。这是在monogame windows uwp项目中完成的。以下代码工作过(我在VS2015社区)。我最近做了一个新的操作系统,并安装VS2017社区。我终于让我的项目建立并运行。唯一不起作用的是Geolocator.RequestAccessAsync抛出一个“在意外时间调用的方法”异常。有任何想法吗?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; 

}); 

回答

0

当您运行RequestAccessAsync在您的应用程序中的第一次,用户将被要求批准您的应用的位置权限。因此该方法需要在UI线程中执行。

+0

这在技术上是答案,但不回答“为什么此代码在之前但不是现在”的问题?我想也许在单人游戏3.5和3.6之间。我没有看到任何证据。对于那些试图在monogame中使用地理位置的人,我必须在托管游戏的XAML页面(GamePage.xaml.cs - UI线程)中启动它。这可能意味着访问状态的变化也可能必须在那里处理。不方便! – Mokgra

相关问题