如何通过一个班级将地理位置传递到另一个班级



我正在尝试通过导航服务传递名为 MyGeoPosition的地理位置变量,但我认为我在某个地方遇到了一个错误之一它给予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);
        }

没有安装电话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.geocoortion

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

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

喜欢他们在这里显示:

developer.nokia.com/community/wiki/how_to_pass_strings_between_pages_on_windows_phone_phone

相关内容

最新更新