如何解决windows phone 8应用程序出现以下错误

本文关键字:应用程序 错误 phone 何解决 解决 windows | 更新日期: 2023-09-27 17:51:05

我正在开发windows phone 8应用程序。

我需要获取用户当前位置的详细信息。

我尝试从MSDN

获取以下代码c#:

 1  private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)
 2   {if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
 3      {
 4               return;
 5       }  
 6       Geolocator geolocator = new Geolocator();
 7       geolocator.DesiredAccuracyInMeters = 50;
 8       try
 9       {
 10           Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromMinutes(5),
               timeout: TimeSpan.FromSeconds(10)
                );
            LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00");
            LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00");
        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                // the application does not have the right capability or the location master switch is off
                StatusTextBlock.Text = "location  is disabled in phone settings.";
            }
            //else
            {
                // something else happened acquring the location
            }
        }
    }

第10行出现错误

'await'操作符只能在异步方法中使用。考虑使用'async'修饰符标记此方法,并将其返回类型更改为'Task'。

我是windows phone应用的新手。现在只有我开始学习基本的WP8。

如何解决windows phone 8应用程序出现以下错误

在你的代码中,你已经使用了await geolocator.GetGeopositionAsync(),现在你只能使用await与异步方法。

所以你要做的是当你使用async方法时只需声明方法aysnc

like: private async void OneShotLocationButton_Click(object sender, RoutedEventArgs e)

代替private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)

按照错误说明操作。把

private void OneShotLocationButton_Click

private async void OneShotLocationButton_Click