开发者

How to pass a variable on pushpin button press?

开发者 https://www.devze.com 2023-04-06 20:57 出处:网络
I am currently using this code to disaply multiple pushpins based on an XML file. I have an onclick event for the pushpins also. But what I would like to do is the pass the Name var from each pushpin

I am currently using this code to disaply multiple pushpins based on an XML file. I have an onclick event for the pushpins also. But what I would like to do is the pass the Name var from each pushpin to another page.

I have set it up the way I thought it would work, but no value is being passed and the message box isn't showing the number from each stop. However when I set the Content of the pushpin to root.Name the number is shown on the pushpin fine.

void busStops_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
        return;

    var busStopInfo = XDocument.Load("Content/BusStops2.xml");

    var Transitresults = from root in busStopInfo.Descendants("Placemark")
                         let StoplocationE1 = root.Element("Point").Element("coordinates")
                         let nameE1 = root.Element("name")

                         select new TransitVariables

                             (StoplocationE1 == null ? null : StoplocationE1.Value,
                                      nameE1 == null ? null : nameE1.Value);

    foreach (var root in Transitresults)
    {
        var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];

        var pin = new Pushpin
        {
            Location = new GeoCoordinate
                {
                    Latitude = root.Lat,
                    Longitude = root.Lon
                },

            Background = accentBrush,
            Content = root.Name,
            Tag = root,             

        };

        pin.MouseLeftButtonUp += BusStop_MouseLeftButtonUp;

        BusStopLayer.AddChild(pin, pin.Location);

    }
}

void BusStop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{       
    var bNumb = ((FrameworkElement)sender);
    if (bNumb != null)
    {
        // Here is would like to pass the Name element from var transitresults to another page. 
         // The element Name is different for each pushpin and is parses from the XML     
    }
}

    // Add properties to your class
    public class TransitVariables
    {
        // Add a constructor:
        public TransitVariables(string stopLocation, string name)
        {
            this.StopLocation = stopLocation;
            this.name = name;
            if (!string.IsNullOrEmpty(StopLocation))
            {
                var items = stopLocation.Split(',');
                this.Lon = double.Parse(items[0]);
                this.Lat = double.Parse(items[1]);
                this.Alt = double.Parse(items[2]);


            }
        }

        public string StopLocation { get; set; }
        public string name { get; set; }
        public double Lat { get; set; }
        public double Lon { get; set; }
        public double Alt { get; set; }



     }
开发者_JAVA百科


You can reference the Pushpin's Tag property inside of your handler:

void BusStop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{       
    var bNumb = ((FrameworkElement)sender);
    if (bNumb != null)
    {
        //bNumb is of type Pushpin
        //bNumb.Tag should have your id in it, I assume it's going to be an XElement object
        string id = "Bus Number Should go here" ;
        NavigationService.Navigate(new Uri("/TestPage.xaml?stopNumber=" + id, UriKind.Relative));
        MessageBox.Show("Stop Number" + id);           
    }
}


Just in case anyone is interested. I worked it out and solved my issue. The key was setting the sender correctly and the tag.

public void BusStop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{

    var item = (sender as Pushpin).Tag as TransitVariables;

    if (item != null)
    {
        string id = item.name;

       NavigationService.Navigate(new Uri("/DepBoard.xaml?ListingId=" + id, UriKind.Relative));
       MessageBox.Show("Bus Stop Number " + id);

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号