From e5c4f9328006140a610163dacaf0ca0740f152a9 Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 11:06:48 +0300 Subject: [PATCH 1/6] ImageEx: Added caching strategy and fixed caching of the Image set by the IsCacheEnabled property. --- .../ImageEx/CachingStrategy.cs | 19 +++ .../ImageEx/ImageExBase.Members.cs | 14 +++ .../ImageEx/ImageExBase.Source.cs | 110 ++++++++++-------- 3 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs new file mode 100644 index 00000000000..6e24cac0898 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs @@ -0,0 +1,19 @@ +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// The type of caching to be applied to . + /// Default is + /// + public enum CachingStrategy + { + /// + /// Caching is handled by 's custom caching system. + /// + Custom, + + /// + /// Caching is handled internally by UWP. + /// + Internal + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs index dd498b861f5..73d84ceffb2 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs @@ -45,6 +45,11 @@ public partial class ImageExBase /// public static readonly DependencyProperty IsCacheEnabledProperty = DependencyProperty.Register(nameof(IsCacheEnabled), typeof(bool), typeof(ImageExBase), new PropertyMetadata(false)); + /// + /// Identifies the dependency property. + /// + public static readonly DependencyProperty CachingStrategyProperty = DependencyProperty.Register(nameof(CachingStrategy), typeof(CachingStrategy), typeof(ImageExBase), new PropertyMetadata(CachingStrategy.Custom)); + /// /// Returns a mask that represents the alpha channel of an image as a /// @@ -125,5 +130,14 @@ public bool IsCacheEnabled get { return (bool)GetValue(IsCacheEnabledProperty); } set { SetValue(IsCacheEnabledProperty, value); } } + + /// + /// Gets or sets a value indicating how the will be cached. + /// + public CachingStrategy CachingStrategy + { + get { return (CachingStrategy)GetValue(CachingStrategyProperty); } + set { SetValue(CachingStrategyProperty, value); } + } } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs index bf654605a75..8f794005eb9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs @@ -121,60 +121,78 @@ private async Task LoadImageAsync(Uri imageUri) { if (_uri != null) { - if (IsCacheEnabled && _isHttpSource) + if (IsCacheEnabled) { - try + switch (CachingStrategy) { - var propValues = new List>(); - - if (DecodePixelHeight > 0) - { - propValues.Add(new KeyValuePair(nameof(DecodePixelHeight), DecodePixelHeight)); - } - - if (DecodePixelWidth > 0) - { - propValues.Add(new KeyValuePair(nameof(DecodePixelWidth), DecodePixelWidth)); - } - - if (propValues.Count > 0) - { - propValues.Add(new KeyValuePair(nameof(DecodePixelType), DecodePixelType)); - } - - var img = await ImageCache.Instance.GetFromCacheAsync(imageUri, true, _tokenSource.Token, propValues); - - lock (LockObj) - { - // If you have many imageEx in a virtualized listview for instance - // controls will be recycled and the uri will change while waiting for the previous one to load - if (_uri == imageUri) - { - AttachSource(img); - ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); - VisualStateManager.GoToState(this, LoadedState, true); - } - } + case CachingStrategy.Custom when _isHttpSource: + await SetHttpSourceCustomCached(imageUri); + break; + case CachingStrategy.Custom: + case CachingStrategy.Internal: + default: + AttachSource(new BitmapImage(imageUri)); + break; } - catch (OperationCanceledException) + } + else + { + AttachSource(new BitmapImage(_uri) { - // nothing to do as cancellation has been requested. - } - catch (Exception e) + CreateOptions = BitmapCreateOptions.IgnoreImageCache + }); + } + } + } + + private async Task SetHttpSourceCustomCached(Uri imageUri) + { + try + { + var propValues = new List>(); + + if (DecodePixelHeight > 0) + { + propValues.Add(new KeyValuePair(nameof(DecodePixelHeight), DecodePixelHeight)); + } + + if (DecodePixelWidth > 0) + { + propValues.Add(new KeyValuePair(nameof(DecodePixelWidth), DecodePixelWidth)); + } + + if (propValues.Count > 0) + { + propValues.Add(new KeyValuePair(nameof(DecodePixelType), DecodePixelType)); + } + + var img = await ImageCache.Instance.GetFromCacheAsync(imageUri, true, _tokenSource.Token, propValues); + + lock (LockObj) + { + // If you have many imageEx in a virtualized listview for instance + // controls will be recycled and the uri will change while waiting for the previous one to load + if (_uri == imageUri) { - lock (LockObj) - { - if (_uri == imageUri) - { - ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); - VisualStateManager.GoToState(this, FailedState, true); - } - } + AttachSource(img); + ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs()); + VisualStateManager.GoToState(this, LoadedState, true); } } - else + } + catch (OperationCanceledException) + { + // nothing to do as cancellation has been requested. + } + catch (Exception e) + { + lock (LockObj) { - AttachSource(new BitmapImage(_uri)); + if (_uri == imageUri) + { + ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e)); + VisualStateManager.GoToState(this, FailedState, true); + } } } } From 7a4969abf135591d8d0ccfc84dd5e8ae55639b1f Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 11:21:59 +0300 Subject: [PATCH 2/6] Small doc update for ImageEx --- docs/controls/ImageEx.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/controls/ImageEx.md b/docs/controls/ImageEx.md index 00fa73bf0e6..5b898224d27 100644 --- a/docs/controls/ImageEx.md +++ b/docs/controls/ImageEx.md @@ -8,7 +8,7 @@ keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp # ImageEx XAML Control The [ImageEx Control](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.uwp.ui.controls.imageex) and [RoundImageEx Control](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.uwp.ui.controls.roundimageex) downloads images asynchronously, while showing a loading indicator. Source images are then stored in the application's local cache to preserve resources and load time. ImageEx also extends the default *Image* and *ImageBrush* Platform controls respectively to improve performance through caching. You can also use a placeholder image that will be displayed while loading the main image. - + ## Syntax ```xaml @@ -33,6 +33,8 @@ On Windows 10.0.16299.0 or higher, `CornerRadius` is supported on ImageEx. Use | Property | Type | Description | | -- | -- | -- | | NineGrid | Thickness | Gets or sets the nine-grid used by the image | +| IsCacheEnabled | Boolean | Gets or sets a value indicating whether gets or sets cache state | +| CachingStrategy | Enum | Gets or sets a value indicating how the Image will be cached | ## RoundImageEx Properties From 535e258b93f72786d4171ad945861b2e510a4501 Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 11:25:11 +0300 Subject: [PATCH 3/6] Types fix according template. --- docs/controls/ImageEx.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/controls/ImageEx.md b/docs/controls/ImageEx.md index 5b898224d27..e390cc3f50d 100644 --- a/docs/controls/ImageEx.md +++ b/docs/controls/ImageEx.md @@ -33,8 +33,8 @@ On Windows 10.0.16299.0 or higher, `CornerRadius` is supported on ImageEx. Use | Property | Type | Description | | -- | -- | -- | | NineGrid | Thickness | Gets or sets the nine-grid used by the image | -| IsCacheEnabled | Boolean | Gets or sets a value indicating whether gets or sets cache state | -| CachingStrategy | Enum | Gets or sets a value indicating how the Image will be cached | +| IsCacheEnabled | bool | Gets or sets a value indicating whether gets or sets cache state | +| CachingStrategy | enum | Gets or sets a value indicating how the Image will be cached | ## RoundImageEx Properties From 7f0ee7c58c93668d0a1b1ce43d09018df42da897 Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 13:55:23 +0300 Subject: [PATCH 4/6] Ran UpdateHeaders.bat --- .../ImageEx/CachingStrategy.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs index 6e24cac0898..d849ee41cb8 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs @@ -1,4 +1,8 @@ -namespace Microsoft.Toolkit.Uwp.UI.Controls +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Toolkit.Uwp.UI.Controls { /// /// The type of caching to be applied to . From fe7343234ff98d2debe8fc62b173332da986a700 Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 23:32:11 +0300 Subject: [PATCH 5/6] Rename CachingStrategy to ImageExCachingStrategy. --- .../ImageEx/CachingStrategy.cs | 2 +- .../ImageEx/ImageExBase.Members.cs | 6 +++--- .../ImageEx/ImageExBase.Source.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs index d849ee41cb8..9db1d3e1ed6 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/CachingStrategy.cs @@ -8,7 +8,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls /// The type of caching to be applied to . /// Default is /// - public enum CachingStrategy + public enum ImageExCachingStrategy { /// /// Caching is handled by 's custom caching system. diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs index 73d84ceffb2..9e4d53bba97 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Members.cs @@ -48,7 +48,7 @@ public partial class ImageExBase /// /// Identifies the dependency property. /// - public static readonly DependencyProperty CachingStrategyProperty = DependencyProperty.Register(nameof(CachingStrategy), typeof(CachingStrategy), typeof(ImageExBase), new PropertyMetadata(CachingStrategy.Custom)); + public static readonly DependencyProperty CachingStrategyProperty = DependencyProperty.Register(nameof(CachingStrategy), typeof(ImageExCachingStrategy), typeof(ImageExBase), new PropertyMetadata(ImageExCachingStrategy.Custom)); /// /// Returns a mask that represents the alpha channel of an image as a @@ -134,9 +134,9 @@ public bool IsCacheEnabled /// /// Gets or sets a value indicating how the will be cached. /// - public CachingStrategy CachingStrategy + public ImageExCachingStrategy CachingStrategy { - get { return (CachingStrategy)GetValue(CachingStrategyProperty); } + get { return (ImageExCachingStrategy)GetValue(CachingStrategyProperty); } set { SetValue(CachingStrategyProperty, value); } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs index 8f794005eb9..90826b23de0 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/ImageEx/ImageExBase.Source.cs @@ -125,11 +125,11 @@ private async Task LoadImageAsync(Uri imageUri) { switch (CachingStrategy) { - case CachingStrategy.Custom when _isHttpSource: + case ImageExCachingStrategy.Custom when _isHttpSource: await SetHttpSourceCustomCached(imageUri); break; - case CachingStrategy.Custom: - case CachingStrategy.Internal: + case ImageExCachingStrategy.Custom: + case ImageExCachingStrategy.Internal: default: AttachSource(new BitmapImage(imageUri)); break; From 4b930c91201be8362d04e99104690284b9ae3cf2 Mon Sep 17 00:00:00 2001 From: Valeriu Balan Date: Thu, 20 Sep 2018 23:35:43 +0300 Subject: [PATCH 6/6] Small doc update for ImageEx --- docs/controls/ImageEx.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/controls/ImageEx.md b/docs/controls/ImageEx.md index e390cc3f50d..d02de0dec03 100644 --- a/docs/controls/ImageEx.md +++ b/docs/controls/ImageEx.md @@ -34,7 +34,7 @@ On Windows 10.0.16299.0 or higher, `CornerRadius` is supported on ImageEx. Use | -- | -- | -- | | NineGrid | Thickness | Gets or sets the nine-grid used by the image | | IsCacheEnabled | bool | Gets or sets a value indicating whether gets or sets cache state | -| CachingStrategy | enum | Gets or sets a value indicating how the Image will be cached | +| ImageExCachingStrategy | enum | Gets or sets a value indicating how the Image will be cached | ## RoundImageEx Properties