2014-11-03 86 views
0

我在這裏有一個小問題,我不知道如何自己解決它。 我想要做的是,我想改變數組(名稱)中每個項目的圖像源。Windows Phone 8 C# - 更改foreach循環中xaml項的名稱

這是我的代碼看起來像現在: (在m.Source M爲其中名稱應該去)

private void Stars() 
    { 
     string[] LevelPick = new string[] {"Stage1Level1", "Stage1Level2", "Stage3Level3" }; 

     foreach (string m in LevelPick) 
     { 
      string Stars = appSettings[""+m+""].ToString(); 

      if(Stars == "0") 
      { 
       BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute)); 
       m.Source = bm; // error here 
      } 
      else 
      { 
       BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute)); 
       m.Source = bm; // same here 
      } 
     } 
    } 

,我得到的錯誤是:

「串'不包含'來源'的定義。

有沒有人知道如何解決這個問題? 在此先感謝!

+0

錯誤消息不明確?由於'm'是'string',因此沒有屬性或字段被稱爲'Source'。你想要做什麼? – 2014-11-03 15:43:10

+0

你應該創建一個Image,這個類有一個Source屬性 – thumbmunkeys 2014-11-03 15:44:26

+0

@thumbmunkeys數組中的名字是圖像: 等 – 2014-11-03 15:48:05

回答

2

你可以簡單地擁有,而不是他們的名字組成的數組圖像控件數組,:

var images = new Image[] { Stage1Level1, Stage1Level2, Stage3Level3 }; 

現在你可以遍歷這些圖像:

foreach (var image in images) 
{ 
    var stars = appSettings[image.Name].ToString(); 

    if (stars == "0") 
    { 
     image.Source = new BitmapImage(new Uri(...)); 
    } 
    ... 
} 
+0

謝謝,這工作:) – 2014-11-03 21:57:30