2014-11-03 84 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