private async void OnUriChanged()
        {
            if (_uri == null)
            {
                ReleaseContent();
                return;
            }

            _surface = await SurfaceLoader.LoadFromUri(_uri);

            if (_loadEffectDelegate != null)
            {
                _effectSurface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);
            }

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

            // Apply stretching policy
            ApplyStretchingPolicy(new Size(ActualWidth, ActualHeight));

            if (_effectBrush == null)
            {
                CompositionSurfaceBrush brush = (CompositionSurfaceBrush)_sprite.Brush;

                if (brush != null)
                {
                    brush.Surface = _surface;
                }
                else
                {
                    _sprite.Brush = _compositor.CreateSurfaceBrush(_surface);
                }
            }
            else
            {
                _effectBrush.SetSourceParameter("ImageSource", _compositor.CreateSurfaceBrush(_surface));

                if (_effectSurface != null)
                {
                    _effectBrush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(_effectSurface));
                }
                _sprite.Brush = _effectBrush;
            }
        }
        public async void SetEffectBrush(CompositionEffectBrush brush)
        {
            // Release previous brush if set
            if (_effectBrush != null)
            {
                _effectBrush.Dispose();
                _effectBrush = null;
            }

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

            _effectBrush = brush;

            // Set the new brush
            if (_sprite != null)
            {
                // Release current brush
                if (_sprite.Brush != null)
                {
                    _sprite.Brush.Dispose();
                }

                if (_surface != null)
                {
                    // If the effect brush is cleared, create a surface brush
                    if (_effectBrush == null)
                    {
                        _sprite.Brush = _compositor.CreateSurfaceBrush(_surface);
                    }
                    else
                    {
                        _effectBrush.SetSourceParameter("ImageSource", _compositor.CreateSurfaceBrush(_surface));

                        if (_loadEffectDelegate != null)
                        {
                            _effectSurface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);

                            _effectBrush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(_effectSurface));
                        }
                        _sprite.Brush = _effectBrush;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// MainPage_Loaded is used for all of our initialization to keep the UI thread mostly free.
        /// </summary>
        /// <param name="sender">not used</param>
        /// <param name="e">not used</param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _compositor   = ElementCompositionPreview.GetElementVisual(sender as UIElement).Compositor;
            _imageFactory = CompositionImageFactory.CreateCompositionImageFactory(_compositor);
            var scrollProperties = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(Scroller);

            SurfaceLoader.Initialize(_compositor);

            ParallaxingImage.LoadTimeEffectHandler = ApplyBlurEffect;
            ParallaxingImage.Uri = new Uri("ms-appx:///assets/RollingWaves.jpg");
            ParallaxingImage.SetEffectBrush(InitializeCrossFadeEffect());

            var maskedBrush = InitializeCompositeEffect();

            ParallaxingImage.SizeChanged += (s, ev) => { UpdateSizes(); };

            InitializeBackgroundImageVisual(scrollProperties);
            InitializeFrontVisual(scrollProperties, maskedBrush);
            InitializeBehindVisual(scrollProperties, maskedBrush);

            UpdateSizes();
        }