Example #1
0
 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     if (_loadedImageSurface != null)
     {
         _loadedImageSurface.Dispose();
         _loadedImageSurface = null;
     }
 }
Example #2
0
 protected override void OnDisconnected()
 {
     if (_surface != null)
     {
         _surface.Dispose();
         _surface = null;
     }
     if (CompositionBrush != null)
     {
         CompositionBrush.Dispose();
         CompositionBrush = null;
     }
 }
Example #3
0
 protected override void OnDisconnected()
 {
     // Dispose Surface and CompositionBrushes if XamlCompBrushBase is removed from tree
     if (_surface != null)
     {
         _surface.Dispose();
         _surface = null;
     }
     if (CompositionBrush != null)
     {
         CompositionBrush.Dispose();
         CompositionBrush = null;
     }
 }
Example #4
0
            public void LoadImage(Uri uri, Size?size = null)
            {
                lock (_lock)
                {
                    if (_surface != null)
                    {
                        _surface.LoadCompleted -= _surface_LoadCompleted;
                        _surface.Dispose();
                        _surface = null;
                    }

                    _loaded = false;
                    if (size.HasValue)
                    {
                        _surface = LoadedImageSurface.StartLoadFromUri(uri, size.Value);
                    }
                    else
                    {
                        _surface = LoadedImageSurface.StartLoadFromUri(uri);
                    }
                    _surface.LoadCompleted += _surface_LoadCompleted;
                }
            }
        /// <summary>
        /// Deconstructs the Composition Brush.
        /// </summary>
        protected override void OnDisconnected()
        {
            // Dispose of composition resources when no longer in use.
            if (CompositionBrush != null)
            {
                CompositionBrush.Dispose();
                CompositionBrush = null;
            }

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

            if (_surface != null)
            {
                _surface.Dispose();
                _surface = null;
            }
        }
Example #6
0
        /// <summary>
        /// Load the image and transform it to a composition brush or a XAML brush (depends of the UIStrategy)
        /// </summary>
        /// <param name="uri">the uri of the image to load</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task <bool> LoadImageBrushAsync(Uri uri)
        {
            if (DesignTimeHelpers.IsRunningInLegacyDesignerMode)
            {
                return(false);
            }

            if (_containerVisual == null || uri == null)
            {
                return(false);
            }

            await _flag.WaitAsync();

            try
            {
                bool isAnimated = IsAnimated;

                IsAnimated = false;

                if (_isImageSourceLoaded)
                {
                    for (int i = 0; i < _compositionChildren.Count; i++)
                    {
                        _compositionChildren[i].Brush = null;
                    }

                    _brushVisual?.Dispose();
                    _brushVisual = null;

                    _imageSurface?.Dispose();
                    _imageSurface = null;
                }

                _isImageSourceLoaded = false;

                var compositor = _containerVisual.Compositor;

                _imageSurface = LoadedImageSurface.StartLoadFromUri(uri);
                var loadCompletedSource = new TaskCompletionSource <bool>();
                _brushVisual = compositor.CreateSurfaceBrush(_imageSurface);

                _imageSurface.LoadCompleted += (s, e) =>
                {
                    if (e.Status == LoadedImageSourceLoadStatus.Success)
                    {
                        loadCompletedSource.SetResult(true);
                    }
                    else
                    {
                        loadCompletedSource.SetException(new ArgumentException("Image loading failed."));
                    }
                };

                await loadCompletedSource.Task;
                _imageSize = _imageSurface.DecodedPhysicalSize;

                _isImageSourceLoaded = true;

                RefreshContainerTile();

                RefreshImageSize(_imageSize.Width, _imageSize.Height);

                if (isAnimated)
                {
                    IsAnimated = true;
                }
            }
            finally
            {
                _flag.Release();
            }

            ImageLoaded?.Invoke(this, EventArgs.Empty);

            return(true);
        }