/// <summary>
        /// Opens from splash screen.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="imageBounds">The image bounds.</param>
        /// <param name="backgroundColor">Color of the background.</param>
        /// <param name="imageUri">The URI of the image.</param>
        public static void OpenFromSplashScreen(this Page page, Rect imageBounds, Color backgroundColor, Uri imageUri)
        {
            page.Loaded += Page_Loaded;

            // Initialize the surface loader
            SurfaceLoader.Initialize(ElementCompositionPreview.GetElementVisual(page).Compositor);

            // Show the custome splash screen
            ShowImage(page, imageBounds, imageUri, backgroundColor);
        }
Ejemplo n.º 2
0
        public CompositionImage()
        {
            this.DefaultStyleKey = typeof(CompositionImage);
            this.Background      = new SolidColorBrush(Colors.Transparent);
            this._stretchMode    = CompositionStretch.Uniform;
            this.Loading        += CompImage_Loading;
            this.Unloaded       += CompImage_Unloaded;
            this.SizeChanged    += CompImage_SizeChanged;

            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            // Intialize the statics as needed
            if (!_staticsInitialized)
            {
                _defaultPlaceholderBrush = _compositor.CreateColorBrush(Colors.DarkGray);

                TimeSpan duration = TimeSpan.FromMilliseconds(1000);
                _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation();
                _fadeOutAnimation.InsertKeyFrame(0, 1);
                _fadeOutAnimation.InsertKeyFrame(1, 0);
                _fadeOutAnimation.Duration = duration;

                _scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
                _scaleAnimation.InsertKeyFrame(0, new Vector2(1.25f, 1.25f));
                _scaleAnimation.InsertKeyFrame(1, new Vector2(1, 1));
                _scaleAnimation.Duration = duration;

                _staticsInitialized = true;
            }

            // Initialize the surface loader if needed
            if (!SurfaceLoader.IsInitialized)
            {
                SurfaceLoader.Initialize(ElementCompositionPreview.GetElementVisual(this).Compositor);
            }

            _placeholderDelay = TimeSpan.FromMilliseconds(50);
            _surfaceBrush     = _compositor.CreateSurfaceBrush(null);
        }
        /// <summary>
        /// Shows the image.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="imageBounds">The image bounds.</param>
        /// <param name="imageUri">The image URI.</param>
        /// <param name="backgroundColor">Color of the background.</param>
        private static async void ShowImage(Page page, Rect imageBounds, Uri imageUri, Color backgroundColor)
        {
            var compositor = ElementCompositionPreview.GetElementVisual(page).Compositor;
            var windowSize = new Vector2((float)Window.Current.Bounds.Width, (float)Window.Current.Bounds.Height);

            //
            // Create a container visual to hold the color fill background and image visuals.
            // Configure this visual to scale from the center.
            //
            var container = compositor.CreateContainerVisual();

            container.Size        = windowSize;
            container.CenterPoint = new Vector3(windowSize.X, windowSize.Y, 0) * .5f;
            ElementCompositionPreview.SetElementChildVisual(page, container);

            //
            // Create the colorfill sprite for the background, set the color to the same as app theme
            //
            var backgroundSprite = compositor.CreateSpriteVisual();

            backgroundSprite.Size  = windowSize;
            backgroundSprite.Brush = compositor.CreateColorBrush(backgroundColor);
            container.Children.InsertAtBottom(backgroundSprite);

            //
            // Create the image sprite containing the splash screen image.  Size and position this to
            // exactly cover the Splash screen image so it will be a seamless transition between the two
            //
            var surface = await SurfaceLoader.LoadFromUri(imageUri);

            var imageSprite = compositor.CreateSpriteVisual();

            imageSprite.Brush  = compositor.CreateSurfaceBrush(surface);
            imageSprite.Offset = new Vector3((float)imageBounds.X, (float)imageBounds.Y, 0f);
            imageSprite.Size   = new Vector2((float)imageBounds.Width, (float)imageBounds.Height);
            container.Children.InsertAtTop(imageSprite);
        }
Ejemplo n.º 4
0
        private async void LoadSurface()
        {
            // If we're clearing out the content, return
            if (_uri == null)
            {
                ReleaseSurface();
                return;
            }

            try
            {
                // Start a timer to enable the placeholder image if requested
                if (_surface == null && _placeholderDelay >= TimeSpan.Zero)
                {
                    _timer          = new DispatcherTimer();
                    _timer.Interval = _placeholderDelay;
                    _timer.Tick    += Timer_Tick;
                    _timer.Start();
                }

                // Load the image asynchronously
                CompositionDrawingSurface surface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);

                if (_surface != null)
                {
                    ReleaseSurface();
                }

                _surface = surface;

                // The surface has changed, so we need to re-measure with the new surface dimensions
                InvalidateMeasure();

                // Async operations may take a while.  If we've unloaded, return now.
                if (_unloaded)
                {
                    ReleaseSurface();
                    return;
                }

                // Update the brush
                UpdateBrush();

                // Success, fire the Opened event
                if (ImageOpened != null)
                {
                    ImageOpened(this, null);
                }

                //
                // If we created the loading placeholder, now that the image has loaded
                // cross-fade it out.
                //

                if (_sprite != null && _sprite.Children.Count > 0)
                {
                    Debug.Assert(_timer == null);
                    StartCrossFade();
                }
                else if (_timer != null)
                {
                    // We didn't end up loading the placeholder, so just stop the timer
                    _timer.Stop();
                    _timer = null;
                }
            }
            catch (FileNotFoundException)
            {
                if (ImageFailed != null)
                {
                    ImageFailed(this, null);
                }
            }
        }