请记住,我对编程非常陌生。我设法将图像设置为 bing 地图上的自定义标记,根据来自 JSON 提要的数据选择图像并将其放置在地图上。我有一个列表视图页面,您可以在其中查看洪水警告,单击此列表中的一个项目会将您带到另一个页面,其中包含该特定洪水的更多详细信息。我想要它,所以当有人点击地图上的标记时,它会将他们带到与该洪水对应的信息页面。(这有点令人费解,但我希望这很清楚)
我很难让它工作,从列表视图导航到信息页面的代码很简单,
private void listBoxBeaches_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
GetSelectedItem();
}
private void GetSelectedItem()
{
RootObject item = listBoxBeaches.SelectedItem as RootObject;
ArDe = item.AreaDescription;
SeTe = item.SeverityTxt;
Rais = item.RaisedF;
MesEng = item.MessageEnglish;
MesCym = item.MessageWelsh;
if (item != null)
{
NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
}
}
但是试图让标记可点击以便导航到同一个地方是有问题的,
public string ArDe;
public string SeTe;
public string Rais;
public string MesEng;
public string MesCym;
public Grid marker;
public NavigationService navServ;
private Map myMap = new Map();
private MapLayer mylayer = new MapLayer();
public Map SetMapPins(List<RootObject>FloodList)
{
myMap.LandmarksEnabled = true;
myMap.PedestrianFeaturesEnabled = true;
myMap.Center = new GeoCoordinate(52.44, -4);
myMap.ZoomLevel = 7.8;
myMap.CartographicMode = MapCartographicMode.Road;
foreach (var flood in FloodList)
{
//this grabs the marker graphic
marker = flood.GetGrid();
MapOverlay myOverlay = new MapOverlay();
myOverlay.GeoCoordinate = new GeoCoordinate(flood.Center.Latitude, flood.Center.Longitude);
string AreaDes = flood.AreaDescription;
string SeverityTxt = flood.SeverityTxt;
string Raised = flood.Raised;
string EngMessage = flood.MessageEnglish;
string CymMessage = flood.MessageWelsh;
marker.MouseLeftButtonUp += (sender, args) => Floodpic_MouseLeftButtonUp(null, null, AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
myOverlay.Content = marker;
mylayer.Add(myOverlay);
}
myMap.Layers.Add(mylayer);
return myMap;
}
private void Floodpic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e, string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
{
GetSelectedMapItem(AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
}
public void GetSelectedMapItem(string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
{
ArDe = AreaDes;
SeTe = SeverityTxt;
Rais = Raised;
MesEng = EngMessage;
MesCym = CymMessage;
//initially I used NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
//but this gives me "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" error
Navigate(navServ, new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}", ArDe, SeTe, Rais, MesEng, MesCym), UriKind.Relative));
}
public void Navigate(NavigationService s, Uri destination)
{
//This is where the AccessViolationException is thrown
s.Navigate(destination);
}
很明显,listview 代码是从实际的 listview 页面 (ListView.xaml.cs) 导航的,而标记代码不在我要导航的页面的 cs 文件中(在 SetMap.cs 中,而不是 MapView.cs 中)。 xaml.cs 地图和标记所在的位置),即它在外部导航。
所以我不知道该怎么做才能克服这个问题,我创建了该Navigation
方法,因为获取对象引用是必需的错误
NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
即使在尝试之后
this.NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
现在我得到了Navigate
调用时抛出的 AccessViolationException 。有任何想法吗?
编辑
我现在已经寻求一个更简单的解决方案(使用 CustomMessageBox,它完成了工作),但我仍然非常感谢这个解决方案。我知道这可能是一个非常具体的问题,因此可能需要一个同样具体的答案。代码有点混乱,但这是由于我缺乏培训或经验。