Ejemplo n.º 1
0
        private async void UpdateSource()
        {
            if (_canvas == null || _isAnimating || _lastSource == Source) // is animating or no change
            {
                return;
            }

            _isAnimating = true;
            var currentSource = Source;

            if (_lastSource == null)
            {
                ForegroundImage.Opacity = 0.0;

                var success = SingleEvent.WaitForEventAsync(ForegroundImage,
                                                            (image, handler) => image.ImageOpened += handler,
                                                            (image, handler) => image.ImageOpened -= handler);

                var failure = SingleEvent.WaitForEventAsync(ForegroundImage,
                                                            (image, handler) => image.ImageFailed += handler,
                                                            (image, handler) => image.ImageFailed -= handler);

                ImageHelper.SetSource(ForegroundImage, currentSource);

                var task = await Task.WhenAny(new[] { success, failure });

                if (task == success)
                {
                    await Fading.FadeInAsync(ForegroundImage, FadingDuration, FadingOpacity);
                }

                // TODO Deregister not called event
            }
            else if (currentSource != null) // exchange images
            {
                BackgroundImage.Opacity = 0.0;

                var success = SingleEvent.WaitForEventAsync(BackgroundImage,
                                                            (image, handler) => image.ImageOpened += handler,
                                                            (image, handler) => image.ImageOpened -= handler);

                var failure = SingleEvent.WaitForEventAsync(BackgroundImage,
                                                            (image, handler) => image.ImageFailed += handler,
                                                            (image, handler) => image.ImageFailed -= handler);

                ImageHelper.SetSource(BackgroundImage, currentSource);

                if (!WaitForNextImage)
                {
                    Fading.FadeOutAsync(ForegroundImage, FadingDuration);
                }

                var task = await Task.WhenAny(new[] { success, failure });

                if (task == success)
                {
                    if (WaitForNextImage)
                    {
                        await Task.WhenAll(new[]
                        {
                            Fading.FadeInAsync(BackgroundImage, FadingDuration, FadingOpacity),
                            Fading.FadeOutAsync(ForegroundImage, FadingDuration)
                        });
                    }
                    else
                    {
                        await Fading.FadeInAsync(BackgroundImage, FadingDuration, FadingOpacity);
                    }

                    ImageHelper.SetSource(ForegroundImage, null);

                    // reverse image order
                    var fore = ForegroundImage;
                    var back = BackgroundImage;
                    _canvas.Children.Clear();
                    _canvas.Children.Add(fore);
                    _canvas.Children.Add(back);
                }
                else
                {
                    await Fading.FadeOutAsync(ForegroundImage, FadingDuration);
                }
            }
            else
            {
                BackgroundImage.Opacity = 0.0;
                await Fading.FadeOutAsync(ForegroundImage, FadingDuration);
            }

            _isAnimating = false;
            _lastSource  = currentSource;
            UpdateSource();
        }