1

I have an 80 PNG image sequence in which I am trying to create an animation for my windows app. The file path is Assets/Star/ and I am trying to figure out how I would make a foreach loop for each image in the folder, so it would set the image object as Image1 then after a certain amount of ticks with the timer it would change it to Image2 and so on, here is what I have so far:

private void SubmitButton_Click(object sender, RoutedEventArgs e)
   {
      if(LevelUp == true)
        {
            string ImagePath = "Assets/Star/";
            foreach (Image item in ImagePath)
            {

            }
        }
   }

However I dont think im approaching it correctly, does anyone know how i should approach this?

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
Jermain Defo
  • 189
  • 1
  • 1
  • 11

1 Answers1

2

Just await Task.Delay to asynchronously wait for a set span of time:

private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    if (LevelUp)
    {
        string imagePath = "Assets/Star/";
        foreach (Image image in GetImages(imagePath))
        {
            ShowImage(image);
            await Task.Delay(timeToWait);
        }
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • Is there a way of setting a time span? – Jermain Defo Feb 11 '15 at 21:23
  • @JermainDefo Yes, of course there is. `timeToWait` in this example is a variable specifying the amount of time to wait. Set it to whatever you want. – Servy Feb 11 '15 at 21:24
  • Wow! I didn't think it would be that easy! So the await Task.Delay is the timer itself? In other words, this will be the solution? – Jermain Defo Feb 11 '15 at 21:26
  • @JermainDefo `Task.Delay` uses a timer internally to create a `Task` that will be completed after the specified delay. You don't need to interact with that timer directly. – Servy Feb 11 '15 at 21:27
  • The GetImages isn't recognised. I have an error stating "The name GetImages does not exist in the current context" do you have any idea why? – Jermain Defo Feb 11 '15 at 21:30
  • @JermainDefo Because you haven't written it yet. You'll need to write the method to get all of the images in a given path. – Servy Feb 11 '15 at 21:31
  • Since the images are named like Image1, Image2, Image3 etc.. Would it not be possible to declare the file path with the image such as "Assets/Images/Image" && number (where the number variable is added by a 1 everytime it comes across a photo) so if 3 was stored in number it would come out as "Assets/Images/Image3", if this is possible to do wouldnt it be better so its in a chronological order? – Jermain Defo Feb 11 '15 at 21:38