5

I need to get a spinning loader to show when my application is busy.

I found a few posts suggesting the MediaElement is the best way to go. I have the following that displays the ajax-loader.gif on the designer. However, when I run the application, the MediaElement doesn't show anything.

<MediaElement Source="file:images/ajax-loader.gif" LoadedBehavior="Play" Visibility="Visible" />

And not sure if this is a related problem, but from the designer, the Source dropdown isn't picking up my images (Build Action = Resource for them). So I manually specified the file which allows it to show in the designer. However, at runtime, the image disappears.

If I specify the image in a static location it works in design and runtime.

C:\temp\ajax-loader.gif

<MediaElement Source="file:/temp/ajax-loader.gif" LoadedBehavior="Play" Visibility="Visible" />

So clearly, even thought I have the Build Action = Resource for my images, they are not getting picked up by the MediaElement. I even tried simply...

<MediaElement Source="ajax-loader.gif" LoadedBehavior="Play" Visibility="Visible" />

How can I use a MediaElement to show an animated GIF that is loaded as a Resource?

John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • I would include it as content and copy to output directory. Did you see this thread though? https://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-work-in-wpf – Andy Apr 17 '18 at 16:30
  • Yes, that's where I saw the suggestion for MediaElement. – John Livermore Apr 17 '18 at 16:39

1 Answers1

9

It turns out MediaElement Source property can't pull a Resource per this article...

https://msdn.microsoft.com/es-es/library/aa970915(v=vs.85).aspx

enter image description here

My solution is...

<MediaElement Source="Images/ajax-loader.gif" LoadedBehavior="Play" Visibility="Visible" />

And set the Build Action = Content...

enter image description here

John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • True, which is why I like the NuGet in [this answer](https://stackoverflow.com/a/11034008/5480409), you can also build your GIF as Resource – Francesco B. Feb 10 '23 at 15:05