C# 如何将地理位置从一个类传递到另一个类?
我试图通过导航服务传递名为C# 如何将地理位置从一个类传递到另一个类?,c#,navigation,C#,Navigation,我试图通过导航服务传递名为MyGeoPosition的GeoPosition变量,但我想我在某个地方出错了,因为我从OnNavigatedTo中提取数据到它提供的另一个类的方法GeoPosition在当前上下文中不存在 在第一类中,我按如下方式传递变量: private async void setLocationBtn_Click(object sender, RoutedEventArgs e) { await this.GetCoordinates
MyGeoPosition
的GeoPosition变量,但我想我在某个地方出错了,因为我从OnNavigatedTo中提取数据到它提供的另一个类的方法GeoPosition在当前上下文中不存在
在第一类中,我按如下方式传递变量:
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);
}
没有安装Phone SDK,但会尝试一下。 看起来您的导航参数有误。给出纬度和长度作为参数,如我的草图所示,然后从参数中恢复您的位置。密钥必须与导航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的情况下键入了它
见:
及
获得价值的更好方法是
TryGetValue()
喜欢他们在这里展示:
developer.nokia.com/community/wiki/How_-to__-to_-pass__-strings_在_-Windows_-Phone上我尝试了上述解决方案,但在声明中
var-mygeopposition=new geocordination(纬度、经度)代码>但它给出了一个错误,指出GeoCordinate接受(double,double)参数..我必须解析为double吗?你能在你的答案中说明如何做到这一点吗?请尝试Double.Parse(NavigationContext.QueryString[“GeoLat”])
,但要注意它不安全-查看并使用TryParse(String,Double)
。
string latitude;
if(NavigationContext.QueryString.TryGetValue(“GeoLat”,out latitude))