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

标签 c# xaml windows-phone-8 geolocation location

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

关于c# - 如何解决 Windows Phone 8 应用程序中的以下错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506800/

相关文章:

c# - WPF 复选框内容不正确

c# - 如何在 Windows Phone 8 中访问 UserControl 的组件/元素

javascript - Windows Phone 8 上的 PhoneGap 和外部链接

c# - 获取当前工作的ip地址

c# - TPL 数据流 block 在 UI 线程上运行

silverlight - Silverlight/XAML 中的水平规则

c# - 动态获取本地化字符串 WP8 c#

c# - 填充特定形状 - 带颜色的 ShapeStyle C# PowerPoint OpenXML

c# - 寻找一种干净的方法将字符串列表转换为 C# 中的有效 List<long>

c# - 托管 C++ dll 中的 XmlnsDefinition