【问题标题】:How To solve the following error in windows phone 8 application?如何解决 windows phone 8 应用程序中的以下错误?
【发布时间】:2014-03-19 13:06:08
【问题描述】:

我正在开发 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 中开始学习基础知识。

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      按照错误说明进行操作。改变

      private void OneShotLocationButton_Click
      

      private async void OneShotLocationButton_Click
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多