Skip to content

Commit

Permalink
CodeQA
Browse files Browse the repository at this point in the history
  • Loading branch information
bagusnl committed Sep 14, 2024
1 parent 754f03a commit d0959d9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 38 deletions.
4 changes: 2 additions & 2 deletions ImageEx/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static class DependencyObjectExtensions
/// Find all ascendant elements of the specified element. This method can be chained with
/// LINQ calls to add additional filters or projections on top of the returned results.
/// <para>
/// This method is meant to provide extra flexibility in specific scenarios and it should not
/// This method is meant to provide extra flexibility in specific scenarios, and it should not
/// be used when only the first item is being looked for. In those cases, use one of the
/// available <see cref="FindAscendant{T}(DependencyObject)"/> overloads instead, which will
/// available <see cref="FindAscendant"/> overloads instead, which will
/// offer a more compact syntax as well as better performance in those cases.
/// </para>
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion ImageEx/ImageEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public partial class ImageEx : ImageExBase
/// Initializes a new instance of the <see cref="ImageEx"/> class.
/// </summary>
public ImageEx()
: base()
{
DefaultStyleKey = typeof(ImageEx);
Debug.WriteLine(DefaultStyleKey.ToString());
Expand Down
8 changes: 5 additions & 3 deletions ImageEx/ImageExBase.Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// ReSharper disable MemberCanBePrivate.Global
namespace ImageEx
{
/// <summary>
Expand Down Expand Up @@ -62,6 +63,7 @@ public partial class ImageExBase
/// <summary>
/// Gets a value indicating whether control has been initialized.
/// </summary>
// ReSharper disable once MemberCanBeProtected.Global
public bool IsInitialized { get; private set; }

/// <summary>
Expand Down Expand Up @@ -101,7 +103,7 @@ public Stretch Stretch
}

/// <summary>
/// Gets or sets a value indicating whether gets or sets cache state
/// Gets or sets a value indicating whether it gets or sets cache state
/// </summary>
public bool IsCacheEnabled
{
Expand All @@ -110,7 +112,7 @@ public bool IsCacheEnabled
}

/// <summary>
/// Gets or sets a value indicating whether gets or sets is lazy loading enable. (17763 or higher supported)
/// Gets or sets a value indicating whether it gets or sets is lazy loading enable. (17763 or higher supported)
/// </summary>
/// <remarks>Windows 10 build 17763 or higher required.</remarks>
public bool EnableLazyLoading
Expand Down Expand Up @@ -148,7 +150,7 @@ private static void EnableLazyLoadingChanged(DependencyObject d, DependencyPrope

private static void LazyLoadingThresholdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ImageExBase control && control.EnableLazyLoading)
if (d is ImageExBase { EnableLazyLoading: true } control)
{
control.InvalidateLazyLoading();
}
Expand Down
34 changes: 10 additions & 24 deletions ImageEx/ImageExBase.Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

namespace ImageEx
{
/// <summary>
Expand All @@ -27,7 +25,7 @@ public partial class ImageExBase
public object Source
{
get { return GetValue(SourceProperty); }
set { Debug.WriteLine("test3"); SetValue(SourceProperty, value); }
set { SetValue(SourceProperty, value); }
}

private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -85,19 +83,17 @@ private void AttachSource(ImageSource source)
VisualStateManager.GoToState(this, LoadedState, true);
ImageExOpened?.Invoke(this, new ImageExOpenedEventArgs());
}
Debug.WriteLine("test4");
}

private async void SetSource(object source)
{
Debug.WriteLine("test5_1");
if (!IsInitialized)
{
return;
}

Debug.WriteLine("test5_2");
_tokenSource?.Cancel();
if (_tokenSource is { Token.IsCancellationRequested: false })
await _tokenSource?.CancelAsync()!;

_tokenSource = new CancellationTokenSource();

Expand All @@ -108,18 +104,14 @@ private async void SetSource(object source)
return;
}

Debug.WriteLine("test5_3");

VisualStateManager.GoToState(this, LoadingState, true);

var imageSource = source as ImageSource;
if (imageSource != null)
{
AttachSource(imageSource);

return;
}
Debug.WriteLine("test5_4");
var uri = source as Uri;
if (uri == null)
{
Expand All @@ -131,14 +123,12 @@ private async void SetSource(object source)
return;
}
}

Debug.WriteLine("test5_6");

if (!IsHttpUri(uri) && !uri.IsAbsoluteUri)
{
uri = new Uri("ms-appx:///" + uri.OriginalString.TrimStart('/'));
}

Debug.WriteLine("test5_7");

try
{
await LoadImageAsync(uri, _tokenSource.Token);
Expand All @@ -152,8 +142,6 @@ private async void SetSource(object source)
VisualStateManager.GoToState(this, FailedState, true);
ImageExFailed?.Invoke(this, new ImageExFailedEventArgs(e));
}

Debug.WriteLine("test5");
}

private async Task LoadImageAsync(Uri imageUri, CancellationToken token)
Expand All @@ -174,7 +162,7 @@ private async Task LoadImageAsync(Uri imageUri, CancellationToken token)
{
var source = imageUri.OriginalString;
const string base64Head = "base64,";
var index = source.IndexOf(base64Head);
var index = source.IndexOf(base64Head, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
var bytes = Convert.FromBase64String(source.Substring(index + base64Head.Length));
Expand All @@ -199,13 +187,11 @@ private async Task LoadImageAsync(Uri imageUri, CancellationToken token)

/// <summary>
/// This method is provided in case a developer would like their own custom caching strategy for <see cref="ImageExBase"/>.
/// By default it uses the built-in UWP cache provided by <see cref="BitmapImage"/> and
/// By default, it uses the built-in UWP cache provided by <see cref="BitmapImage"/> and
/// the <see cref="Image"/> control itself. This method should return an <see cref="ImageSource"/>
/// value of the image specified by the provided uri parameter.
/// A <see cref="CancellationToken"/> is provided in case the current request is invalidated
/// (e.g. the container is recycled before the original image is loaded).
/// The Toolkit also has an image cache helper which can be used as well:
/// <see cref="CacheBase{T}.GetFromCacheAsync(Uri, bool, CancellationToken, List{KeyValuePair{string, object}})"/> in <see cref="ImageCache"/>.
/// (e.g. the container is recycled before the original image loaded).
/// </summary>
/// <example>
/// <code>
Expand Down Expand Up @@ -234,7 +220,7 @@ private async Task LoadImageAsync(Uri imageUri, CancellationToken token)
/// <returns><see cref="Task"/></returns>
protected virtual Task<ImageSource> ProvideCachedResourceAsync(Uri imageUri, CancellationToken token)
{
// By default we just use the built-in UWP image cache provided within the Image control.
// By default, we just use the built-in UWP image cache provided within the Image control.
return Task.FromResult((ImageSource)new BitmapImage(imageUri));
}
}
Expand Down
10 changes: 4 additions & 6 deletions ImageEx/ImageExBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
// ReSharper disable MemberCanBePrivate.Global

namespace ImageEx
{
Expand Down Expand Up @@ -80,10 +80,8 @@ public abstract partial class ImageExBase : Control
/// <summary>
/// Initializes a new instance of the <see cref="ImageExBase"/> class.
/// </summary>
public ImageExBase()
{
Debug.WriteLine("test2");
}
// ReSharper disable once PublicConstructorInAbstractClass
public ImageExBase() { }

/// <summary>
/// Attach image opened event handler
Expand Down Expand Up @@ -157,7 +155,7 @@ protected override void OnApplyTemplate()
RemoveImageOpened(OnImageOpened);
RemoveImageFailed(OnImageFailed);

Image = GetTemplateChild(PartImage) as object;
Image = GetTemplateChild(PartImage);

IsInitialized = true;

Expand Down
2 changes: 1 addition & 1 deletion ImageEx/Themes/Generic.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///ImageEx/ImageEx.xaml" />
<ResourceDictionary Source="ms-appx:///ImageEx.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
1 change: 0 additions & 1 deletion ImageEx/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#endif

#if NETCOREAPP
global using Microsoft.UI.Composition;
global using Microsoft.UI.Xaml;
global using Microsoft.UI.Xaml.Controls;
global using Microsoft.UI.Xaml.Media;
Expand Down

0 comments on commit d0959d9

Please sign in to comment.