Search code examples
c#silverlightwindows-phone-7bing-mapspushpin

Passing parameters from one page to another working with pushpins and objects


I'm building an application for Windows Phone 7, I need to print pushpins in a map, so I've done that. I read the data from the database response.

The issue is...every pushpins means a place, but I don't know how to add the place to the pushpin or send the business properties to the other page, where I am going to write them down in some textblocks.

void app_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        Place business = new Place();
        if (e.Error == null)
        {
            XDocument feedXml = XDocument.Parse(e.Result);
            foreach (var properties in feedXml.Descendants("RESULT"))
            {
                Pushpin p = new Pushpin();
                p.Location = new GeoCoordinate((double)properties.Element("LATITUDE"), (double)properties.Element("LONGITUDE"));
                p.Content = properties.Element("NAME").Value;
                p.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(pushPin_Tap);
                p.DoubleTap += new EventHandler<System.Windows.Input.GestureEventArgs>(pushPin_DoubleTap);

                business.Id = (int)properties.Element("ID");
                business.Category = properties.Element("CATEGORY").Value;
                business.Name = properties.Element("NAME").Value;
                business.Street = properties.Element("STREET").Value;
                business.City = properties.Element("CITY").Value;
                business.Neighborhood = properties.Element("NEIGHBORHOOD").Value;
                business.State = properties.Element("STATE").Value;
                business.ZipCode = properties.Element("ZIPCODE").Value;
                business.Country = properties.Element("COUNTRY").Value;
                business.Hours = properties.Element("HOURS").Value;
                business.Description = properties.Element("DESCRIPTION").Value;
                business.Images = properties.Element("IMAGES").Value;
                business.Latitude = (double)properties.Element("LATITUDE");
                business.Longitude = (double)properties.Element("LONGITUDE");
                business.Payment = "Nope";
                business.Distance = properties.Element("DISTANCE").Value;


                map1.Children.Add(p);


            }
        }
    }

Then I use the method to send some parameters to other page, but the pushpin values, not the busineess, which I need..

void pushPin_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        e.Handled = true;


        var businessinfo = ((FrameworkElement) sender).Tag as Place;

        /*if (businessinfo != null)
        {
            string name = businessinfo.Name;
            string category = businessinfo.Category;
            string address = businessinfo.Street;
            string distance = businessinfo.Distance;
            string payment = businessinfo.Payment;
            string hours = businessinfo.Hours;

            NavigationService.Navigate(new Uri(
            "/BusinessInfoPage.xaml?name=" + name + 
            "&category=" + category +
            "&address=" + address + 
            "&distance=" + distance + 
            "&payment=" + payment + 
            "&hours=" + hours, UriKind.Relative));

        }
         */

        NavigationService.Navigate(new Uri("/BusinessInfoPage.xaml?name=Oxxo&category=Convenience&address=Somewhere&distance=.005km&payment=Nothing&hours=8am to 9pm", UriKind.Relative));


    }

Any help or recommendation? Thanks :)


Solution

  • You may use PhoneApplicationService.Current.State to store and retrieve data between pages. For example in Page A:

    Place loc = new Place();
    //fill the Place properties
    PhoneApplicationService.Current.State["place"]=loc;
    //Navigate to Page B
    

    In Page B inside the OnNavigatedTo:

    Place place = PhoneApplicationService.Current.State["place"];
    

    You may want to add some Exception catching in there and make sure the key "pins" exists in the State collection.

    Yo can also use a public property in the App.xaml.cs.

    public Place place {get;set;}
    

    In Page A:

    var obj = App.Current as App;
    obj.place = aPlace; //assumming aPlace is already created
    

    In Page B, OnNavigatedTo:

    var obj = App.Current as App;
    aPlace = obj.place; //assuming you have aPlace declared before