2011-07-19 78 views
1

我正在使用Silverlight的Bing地圖API,並且需要在地圖上循環選擇圖釘並執行一些格式化引腳名稱不等於代碼中其他位置的值。使用uielement.GetValue(),錯誤「非靜態字段需要對象引用」

下面是函數:

private void deselectAllPins(MapLayer mainMapLayer) 
    { 
     foreach (UIElement ui in mainMapLayer.Children) 
      if (ui is SitePushpin) 
      { 
       SolidColorBrush scb = new SolidColorBrush(Colors.Red); 

       if (ui.GetValue(SitePushpin.SiteName).ToString() != hiddenShortName.Text) 
       { 
        ....formatting 
       } 
      } 
    } 

的 'SitePushpin' 被聲明爲:

public class SitePushpin : Pushpin 
{ 
    public string SiteName { get; set; } 
    public SitePushpin(Color Bg) 
    { 
     this.Name = Guid.NewGuid().ToString(); 
     SolidColorBrush scb = new SolidColorBrush(Bg); 
     this.Background = scb; 
    } 
} 

我的問題在與ui.GetValue if語句的用武之地。這是無法看到網站名稱屬性,並與錯誤

來了「是必需的非靜態字段的對象引用」

任何想法如何,我可以得到它看到這個價值?

非常感謝 帽

+0

它看起來像你正在使用屬性訪問的變量名稱。這是正確的,還是你的意思是通過依賴屬性訪問名爲SiteName的屬性?下面的「答案」可能會編譯(不知道SiteName如何不是DP),但實際的代碼對我來說還沒有意義(還)。你能澄清一切嗎? –

回答

1

物業SiteName是一個實例屬性,而不是type/static所以你應該有SitePushpin類的實例,然後訪問SiteName財產。

SO

var pushpin = ui as SitePushpin; 
if (pushpin != null) 
{ 
    SolidColorBrush scb = new SolidColorBrush(Colors.Red); 
    if (ui.GetValue(pushpin.SiteName).ToString() != hiddenShortName.Text)         
    { 
     ....formatting     
    } 
} 
+0

spot on,thank you :) – Captastic

+0

@Captastic:cheers;) – sll

-2

更改此:

if (ui.GetValue(SitePushpin.SiteName).ToString() 

這樣:

if (ui.GetValue(ui.SiteName).ToString() 

,甚至這樣的:

if (ui.SiteName != hiddenShortName.Text) 
+0

ui是UIElement類型 – sll

+0

'if(ui是SitePushpin)' – MusiGenesis

+0

它只是檢查對象是否是特定類型但不能強制轉換 – sll

相關問題