Пример #1
0
        private static void CloseAnimationOrImage(Image?imageControl)
        {
            if (imageControl == null)
            {
                return;
            }

            ImageSource source = GetAnimatedSource(imageControl);

            if (source != null)
            {
                AnimationCache.DecrementReferenceCount(source, GetRepeatBehavior(imageControl));
            }

            ImageAnimationController controller = GetAnimationController(imageControl);

            if (controller != null)
            {
                controller.Dispose();
            }

            SetAnimationController(imageControl, null);
            SetIsAnimationLoaded(imageControl, false);
        }
Пример #2
0
        private static void InitAnimationOrImage(Image?imageControl)
        {
            if (imageControl == null)
            {
                return;
            }

            var  source              = GetAnimatedSource(imageControl) as BitmapSource;
            bool isInDesignMode      = DesignerProperties.GetIsInDesignMode(imageControl);
            bool animateInDesignMode = GetAnimateInDesignMode(imageControl);
            bool shouldAnimate       = !isInDesignMode || animateInDesignMode;

            // For a BitmapImage with a relative UriSource, the loading is deferred until
            // BaseUri is set. This method will be called again when BaseUri is set.
            bool isLoadingDeferred = IsLoadingDeferred(source);

            if (source != null && shouldAnimate && !isLoadingDeferred)
            {
                // Case of image being downloaded: retry after download is complete
                if (source.IsDownloading)
                {
                    EventHandler?handler = null;
                    handler = (sender, args) =>
                    {
                        source.DownloadCompleted -= handler;
                        InitAnimationOrImage(imageControl);
                    };
                    source.DownloadCompleted += handler;
                    imageControl.Source       = source;
                    return;
                }

                ObjectAnimationUsingKeyFrames?animation = GetAnimation(imageControl, source);
                if (animation != null)
                {
                    if (animation.KeyFrames.Count > 0)
                    {
                        // For some reason, it sometimes throws an exception the first time... the second time it works.
                        TryTwice(() => imageControl.Source = (ImageSource)animation.KeyFrames[0].Value);
                    }
                    else
                    {
                        imageControl.Source = source;
                    }

                    RepeatBehavior repeatBehavior = GetRepeatBehavior(imageControl);
                    bool           synchronized   = GetSynchronizedBySource(imageControl);
                    AnimationCache.IncrementReferenceCount(source, repeatBehavior);
                    AnimationClock?clock;
                    if (synchronized)
                    {
                        clock = AnimationCache.GetClock(source, repeatBehavior);
                        if (clock == null)
                        {
                            clock = animation.CreateClock();
                            AnimationCache.AddClock(source, repeatBehavior, clock);
                        }
                    }
                    else
                    {
                        clock = animation.CreateClock();
                    }
                    var controller = new ImageAnimationController(imageControl, animation, clock);
                    SetAnimationController(imageControl, controller);
                    SetIsAnimationLoaded(imageControl, true);
                    imageControl.RaiseEvent(new RoutedEventArgs(AnimationLoadedEvent, imageControl));
                    return;
                }
            }
            imageControl.Source = source;
        }