NavigationService 在 Windows Phone 8 上的 c# 中抛出 AccessViolatio
本文关键字:AccessViolatio 上的 Windows Phone NavigationService | 更新日期: 2023-09-27 18:36:45
请记住,我对编程很陌生。我已经设法将图像设置为必应地图上的自定义标记,根据来自 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.xaml.cs)导航的,而标记代码不在我正在导航的页面的cs文件中(在SetMap.cs中,而不是MapView.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
时抛出的访问违规异常。有什么想法吗?
编辑
我现在已经采用了更简单的解决方案(使用 CustomMessageBox,它可以完成工作),但我仍然非常感谢对此的解决方案。我知道这可能是一个非常具体的问题,因此可能需要一个同样具体的答案。代码有点混乱,但那是由于我缺乏培训或经验。
如果您的页面位于根目录上,请尝试此路径。
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));
不能在构造函数中调用NavigationService.Navigate
。
或如果您正在从usercontrol
RootFrame.Navigate(new Uri(string.Format("~/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));