XamlAnimatedGif / WpfAnimatedGif

A simple library to display animated GIF images in WPF, usable in XAML or in code.
Apache License 2.0
664 stars 125 forks source link

Hide then Show again, animation no longer works #48

Closed Gwunhar closed 6 years ago

Gwunhar commented 6 years ago

I'm setting an Image visible when I launch a background worker and using an animated gif in it and hiding it when the work completes. It works great the first time through, but if I click the "do work" button again when the image is set visible again the animation doesn't work. The only remarkable thing I'm doing in between is re-setting the UriSource of the image.

Is this a bug or am I doing something wrong?

thomaslevesque commented 6 years ago

Hi @Gwunhar,

I'm not sure... Could you show some of the code?

Gwunhar commented 6 years ago

Sorry for the delayed response!

So on button click I pick a random gif to show while I go do work on a background worker:

Random rdm = new Random();
int pic = rdm.Next(1, 30);
var image = new BitmapImage();
image.BeginInit();
if (pic >= 1 && pic <= 10)
{ image.UriSource = new Uri("/Images/Corgi Treadmill.gif", UriKind.Relative); }
else if (pic >= 11 && pic <= 20)
{ image.UriSource = new Uri("/Images/Pug Couch.gif", UriKind.Relative); }
else if (pic >= 21 && pic <= 30)
{ image.UriSource = new Uri("/Images/Pug Stairs.gif", UriKind.Relative); }
image.EndInit();
ImageBehavior.SetAnimatedSource(imgWorking, image);
ImageBehavior.SetAutoStart(imgWorking, true);
ImageBehavior.SetRepeatBehavior(imgWorking, RepeatBehavior.Forever);

cOverflow.Visibility = Visibility.Visible;
imgWorking.Visibility = Visibility.Visible;
//throw a background worker here

Which works perfectly and the team gets to watch doggies while their files are being made. And when the worker completes I do this

imgWorking.Visibility = Visibility.Hidden;
MessageBox.Show("Production complete!", "Production complete", MessageBoxButton.OK, MessageBoxImage.Information);
tbJobNumber.IsEnabled = true;
btnMakeProduction.IsEnabled = true;
btnRefresh.IsEnabled = true;
btnSetSimplex.IsEnabled = true;
cOverflow.Visibility = Visibility.Hidden;

And everything's good.

However, if I immediately click the same "do work" button again I'll get a static display of whichever gif gets picked instead of an animated one.

thomaslevesque commented 6 years ago

Hi @Gwunhar,

Thanks for the details. This looks like a bug in the way relative URIs are handled. I have a fix coming, but in the meantime you can use absolute URIs as a workaround (e.g. pack://application:,,,/Images/Corgi Treadmill.gif).

thomaslevesque commented 6 years ago

@Gwunhar, the bug is fixed in 1.4.17 https://www.nuget.org/packages/WpfAnimatedGif/1.4.17

Gwunhar commented 6 years ago

Thanks for the quick response and fix! I tested it and it's working perfectly.