如何将GeoPosition从一个类传递到另一个类?

时间:2014-03-11 08:53:58

标签: c# navigation

我正在尝试通过导航服务传递一个名为MyGeoPosition的GeoPosition变量,但我认为我在某处遇到了错误之一,就像我从其他类的OnNavigatedTo方法中提取数据一样给出geopositon doesn't exist in the current context

在第一课我传递这样的变量:

private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
        { 
            await this.GetCoordinates();
            //NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
            this.NavigationService.Navigate(new Uri(string.Format("/Maps.xaml?GeoX={0}&GeoY={1}", MyGeoPosition), UriKind.Relative));
        }

获取位置的方法:

    private async Task GetCoordinates(string name = "My Car")
    {
        await Task.Run(async () =>
        {
            // Get the phone's current location.
            Geolocator MyGeolocator = new Geolocator();
            MyGeolocator.DesiredAccuracyInMeters = 5;
            Geoposition MyGeoPosition = null;

            try
            {
                MyGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));

            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
            }
            catch (Exception ex)
            {
                // Something else happened while acquiring the location.
                MessageBox.Show(ex.Message);
            }
        });
    }

我想要检索变量数据的第二类:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.ContainsKey("MyGeoPosition"))
            {
              var MyGeoPosition = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude); 
            }


            base.OnNavigatedTo(e);
        }

1 个答案:

答案 0 :(得分:1)

没有安装Phone SDK,但会尝试一下。 看起来你对naviagtion参数有误。将纬度和经度作为参数(如草图中所示),然后从参数中恢复您的位置。密钥必须与导航uri中的参数相匹配。

第一课:

this.NavigationService.Navigate(new Uri(string.Format("/Maps.xaml?GeoLat={0}&GeoLong={1}", MyGeoPosition.Coordinate.Latitude,MyGeoPosition.Coordinate.Longitude), UriKind.Relative));

第二课:

   if (NavigationContext.QueryString.ContainsKey("GeoLat") && NavigationContext.QueryString.ContainsKey("GeoLong"))
            {
              var latitude = NavigationContext.QueryString["GeoLat"];
              var longtitude = NavigationContext.QueryString["GeoLong"];
              var MyGeoPosition = new GeoCoordinate(latitude , longtitude ); 
            }

您可能需要修复一些小错误,我在没有VS的情况下键入它。

请参阅: http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation  和 http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geocoordinate

获得价值的更好方法是 TryGetValue()

string latitude;
    if(NavigationContext.QueryString.TryGetValue(“GeoLat”,out latitude))

喜欢他们显示在这里:

developer.nokia.com/community/wiki/How_to_pass_strings_between_pages_on_Windows_Phone