Windows Phone 8 C#-更改foreach循环中xaml项目的名称

本文关键字:项目 xaml 循环 Phone C#- 更改 foreach Windows | 更新日期: 2023-09-27 18:28:51

我有一个小问题,我不知道如何自己解决。我想做的是,我想更改数组中每个项目(名称)的图像源。

这就是我的代码现在的样子:(m处的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
            }
        }
    }

我得到的错误是:

"string"不包含"Source"的定义。

有人知道怎么解决这个问题吗?提前感谢!

Windows Phone 8 C#-更改foreach循环中xaml项目的名称

您可以简单地拥有一个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(...));
    }
    ...
}