如何在图钉按钮上传递变量?

时间:2011-09-23 18:29:39

标签: c# silverlight windows-phone-7

我目前正在使用此代码来删除基于XML文件的多个图钉。我也有针对图钉的onclick事件。但我想要做的是将每个图钉的Name var传递给另一个页面。

我已按照我认为可行的方式进行设置,但没有传递任何值,并且消息框未显示每个停靠点的数字。但是,当我将图钉的内容设置为root.Name时,数字显示在图钉上。

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



     }

2 个答案:

答案 0 :(得分:0)

您可以在处理程序中引用Pushpin的Tag属性:

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

答案 1 :(得分:0)

以防万一有兴趣。我解决了问题并解决了我的问题。关键是正确设置发件人和标签。

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

    }
}