2011-09-23 3 views
0

현재이 코드를 사용하여 XML 파일을 기반으로 여러 압핀을 사용하지 않습니다. 압정을위한 onclick 이벤트도 있습니다. 하지만 내가하고 싶은 것은 Name var을 각 고정 핀에서 다른 페이지로 패스하는 것입니다.압정 버튼을 눌렀을 때 변수를 전달하는 방법?

나는 그것이 작동 할 것이라고 생각하는 방식으로 설정했지만 값은 전달되지 않고 메시지 상자는 각 정지 지점의 번호를 표시하지 않습니다. 그러나 내가 pushpin의 내용을 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; } 



    } 
+0

실제로 컴파일 할 수 있도록 코드를 정리할 수 있습니까? 예를 들어 이벤트 코드에서 루트를 사용하고 있지만 bNumb을 의미한다고 생각하십니까? – AnthonyWJones

+0

@AnthonyWJones bNumb.Name은 루트에서 파싱 된 이름이 아니라 속성 이름이나 bNUmb를 가리 키기 때문에 잘못되었습니다. – Rhys

+0

질문은 여전히 ​​혼란 스럽습니다. 이벤트 핸들러는 ID를 정적 문자열로 하드 코딩하므로 "메시지 상자가 각 정류장의 번호를 표시하지 않음"이란 무엇을 의미합니까? – Bryant

답변

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

    } 
} 
0

당신은 핸들러 내부의 고정 핀의 태그 속성을 참조 할 수 있습니다 : 사람이 관심

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

망고에 'MouseLeftButtonUp' 대신에'Tap' 이벤트가 있습니다. –

+0

@ Casey Margell 어떻게 태그 속성을 각 압핀마다 다른 XML 구문 분석 값'name'으로 설정하겠습니까? – Rhys

관련 문제