Skip to main content
 首页 » 编程设计

c#之如何解决 Windows Phone 8 应用程序中的以下错误

2024年02月13日11kenshinobiy

我正在开发 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' 运算符只能在异步方法中使用。考虑使用“异步”修饰符标记此方法并将其返回类型更改为“任务”。

我是 Windows Phone 应用程序的新手。现在只有我开始学习 WP8 中的基础知识。

请告诉我如何解决这个...

请您参考如下方法:

在您的代码中您使用了 await geolocator.GetGeopositionAsync() 现在您只能将 await 与异步方法一起使用。

所以你要做的就是每当你使用异步方法时只声明方法aysnc

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

而不是 private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)