XamlAnimatedGif / WpfAnimatedGif

A simple library to display animated GIF images in WPF, usable in XAML or in code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullReferenceException after setting animated source in C# code

justfony opened this issue · comments

I set animated source in code, but after this manual control attemts bring to NullRefernceException
ImageBehavior.SetAnimatedSource(im, r_down);
ImageBehavior.GetAnimationController(im).Play();

Where does r_down come from? How did you load it?
GetAnimationController returns a controller only if the image is animated; and depending on how you load the image (e.g. if you load it from a remote URL), it might not be available immediately (you can subscribe to the AnimationLoaded event to be notified when the animation is done loading).
In any case, you can't assume that GetAnimationController will always return a value, so you need to check it's return value before you use it.

I load image in the constructor of the window.

        r_down = new BitmapImage();
        r_down.BeginInit();
        r_down.UriSource = new Uri("wheel_right.gif", UriKind.Relative);
        r_down.EndInit();

And on buttonclick I set it as animatedsource of image control

 private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        ImageBehavior.SetAnimatedSource(im, r_down);
       //ImageBehavior.GetAnimationController(im).Play();
    }

Are you sure the image is correctly loaded? Where is the image file? You're using a relative URI, so it's looking for the file in the working directory. Is this really what you want?

Image is loaded correctly, because if autostart property is set true, image animates right after setting the animatedsource (if there is no call to animation controller). Images is in the working directory and this is what i really want.

OK, I can reproduce the problem, but I can't understand what is happening without diving into the code. I'll try to have a look tonight.

OK, I found where the issue is coming from, but I don't have an immediate fix.

The root cause of the problem is that you're using a relative URI. Typically, when you do that in XAML, the runtime sets the BaseUri property automatically; if the BaseUri property isn't set yet, the IsLoadingDeferred method returns true, because it assumes that BaseUri is going to be set later, which will cause InitAnimationOrImage to be executed again. But in this case the BaseUri is never set, so the animation isn't initialized.

I'm not sure how to fix that yet; the code that checks for BaseUri is there for a good reason, I can't just remove it. But I have no way to distinguish between

  • a BitmapImage with a relative URI set in XAML, which refers to an image in the resources (URI relative to BaseUri)
  • a BitmapImage with a relative URI set in code, which refers to a file (URI relative to the current folder)

So, the short term solution for your problem is to specify an absolute URI.

I also noticed another issue, where the Play method has no effect if called immediately after setting the AnimatedSource. I know how to fix it, I'll publish a new version shortly. However, if you want to start the animation immediately after setting the AnimatedSource, it probably doesn't make sense to set AutoStart to false ;)

Thanks for answer. I know what Autostart property means)) I set it true only in order to find out whether image loads correctly and animates or not.