[英]Windows Phone 8 C# - Change the name of an xaml item in a foreach loop
我这里有一个小问题,我自己也不知道该如何解决。 我想做的是,我想更改数组(名称)中每个项目的图像源。
这是我的代码现在的样子:(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
}
}
}
我得到的错误是:
“字符串”不包含“源”的定义。
有人知道如何解决此问题吗? 提前致谢!
您可以只包含一个Image控件数组,而不是它们的名称数组:
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(...));
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.