0

Bear in mind I'm very new to programming. I've managed to set up images as custom markers on bing maps, the images are chosen and placed on the map according to data coming from a JSON feed. I have a list view page where one can view flood warnings, and clicking on an item in this list will take you to another page with further details of that particular flood. I want it so when someone clicks on a marker on the map it will take them to the info page corresponding to that flood. (It's a bit convoluted but I hope this is clear)

I've had great difficulty to get this to work, the code for navigating from the listview to the info page is simple enough,

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

But in attempting to get the markers clickable so that it navigates to the same place has been problematic,

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

Just so it's clear, the listview code is navigating from the actual listview page (ListView.xaml.cs), while the marker code is not in the cs file of the page I'm navigating from (in SetMap.cs, not MapView.xaml.cs where the map and markers are) i.e. it navigates externally.

So I'm not sure what to do to get past this, I created the Navigation method due to getting an object reference is required error for

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

even after trying

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

Now I'm getting the AccessViolationException thrown when Navigate is called. Any ideas?

EDIT
I've gone for a simpler solution for now (using CustomMessageBox, it gets the job done) but I'd still greatly appreciate a solution to this. I understand that this might be an incredibly specific problem and thus might require an equally specific answer. The code is a bit mish mashed but that's due to my lack of training or experience.

Marushiru
  • 43
  • 6

1 Answers1

0

Try this path if your page is on 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));

you can not call NavigationService.Navigate in constructor.

OR if your are navigating from 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));
Ajay
  • 6,418
  • 18
  • 79
  • 130
  • I get the same error as I do with `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));` I get "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" – Marushiru Jan 08 '15 at 11:05
  • `~` symbol. I think you mis this – Ajay Jan 08 '15 at 11:33
  • Yes, in the question I state that I tried it without. Adding the `~` gives me the same error "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" – Marushiru Jan 08 '15 at 11:43
  • I am saying to add this. – Ajay Jan 08 '15 at 12:06
  • I'm saying I have added it, doing so gives me the exact same problem. `NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mesc‌​ym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));` and `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));` give me the "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" error – Marushiru Jan 08 '15 at 13:22
  • Besides that it does not work with the `~`, `NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mesc‌​‌​ym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));` works fine when used in ListView. – Marushiru Jan 08 '15 at 16:21