NavigationService在Windows Phone 8上的c#中抛出AccessViolationException

时间:2015-01-07 11:16:56

标签: c# windows-phone-8 access-violation navigationservice

请记住,我对编程非常陌生。我设法在bing地图上将图像设置为自定义标记,根据来自JSON Feed的数据选择图像并将其放置在地图上。我有一个列表视图页面,可以查看洪水警告,点击此列表中的项目将带您到另一个页面,其中包含该特定洪水的更多详细信息。我想要它,所以当有人点击地图上的标记时,它会将它们带到与该洪水相对应的信息页面。 (这有点令人费解,但我希望这很清楚)

我很难让它工作,从listview导航到信息页面的代码很简单,

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.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,它可以完成工作),但我仍然非常感谢解决方案。我知道这可能是一个非常具体的问题,因此可能需要一个同样具体的答案。代码有点混乱,但这是由于我缺乏培训或经验。

1 个答案:

答案 0 :(得分:0)

如果您的网页位于root用户,请尝试使用此路径。

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));