2011-01-22 47 views
0

不知道,如果標題是真正正確的,但是這是問題...配售方法

我有一個foreach循環填充一個圖釘集合。我需要在其中一個XML元素(Latitude)上做一些「工作」。 '工作'被註釋掉了。問題是我不知道在哪裏把這些任務/變量放在任何地方。無論我放置在哪裏,我都會遇到各種各樣的錯誤。如果需要,我可以詳細介紹一下,但是想知道是否有更好的方法來做到這一點。它的優點自我解釋下面我試圖做什麼,但讓我知道如果你需要更多的解釋。歡迎任何建議。

public void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     var document = XDocument.Load(e.Result); 
     if (document.Root == null) 
      return; 
     var events = from ev in document.Descendants("item") 

         .Where(ev => ev.Element("category") != null) 

        select new 
        { 

         Title = (ev.Element("title").Value), 
         Description = (ev.Element("description").Value), 
         Category = (ev.Element("category").Value), 
         Latitude = (ev.Element("link").Value), 

         }; 


     ObservableCollection<Eng> pushPinCollection = new ObservableCollection<Eng>(); 

     foreach (var ev in events) 
     { 

      Eng PushPin = new Eng 
       (ev.Title, ev.Description, ev.Category) 

      // Lat1 = (ev.Latitude), 
      // var items = Lat1.Split('?')[1].Split('&').Select(i => i.Split ('=')).ToDictionary(o => o[0], o => o[1]); 
      // var lon = items["lon"]; 
      // var lat = items["lat"]; 
      // var lat1 = Convert.ToDouble(lat); 
      // var lon1 = Convert.ToDouble(lon); 
      { 

     //Location value below is System.Device.location.Geocoordinate Eng Location 
        Location = new GeoCoordinate(lat1, lon1), 
        Title = ev.Title, 
        Description = ev.Description, 
        Category = ev.Category 

      }; 
      pushPinCollection.Add(PushPin); 
     } 
     pushPins = pushPinCollection; 
     mapItems.ItemsSource = PushPins; 

     } 
+0

什麼「錯誤varity」你看見了什麼? – Keltex 2011-01-22 23:31:32

+0

實際上,我基本上已經試過每一個位置組合,而且;除了工作之外! – 2011-01-22 23:34:29

回答

1

一般來說......你應該在「最窄」範圍內聲明變量。但是,如果您要訪問For循環外部的變量,則需要在For循環之外聲明它們。可能只是在「foreach(var ev in events)」之上,應該是OK。

1

您不能在構造函數調用結束和初始化程序之間插入任何代碼。有幾種方法來解決這個問題:

Eng pushPin = new Eng(...); // note the semicolon 

// calculations to get lat1 and lon1 

pushPin.Location = new GeoCoordinate(lat1, lon1); // Location must have a public Set 

// calculations for lat1 and lon 1 

Eng PushPin = new Eng(ev.Title, ev.Description, ev.Category) 
        { 
         Location = new GeoCoordinate(lat1, lon1); 
        } 

,或者如果你有一個構造函數會有地理座標:

Eng PushPin = new Eng(ev.Title, ev.Description, ev.Category, new GeoCoordinate(lat1, lon1));