Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions AsyncImageLoader.Avalonia/ImageBrushLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ private static async void OnSourceChanged(ImageBrush imageBrush, AvaloniaPropert

Bitmap? bitmap = null;
try {
if (!string.IsNullOrWhiteSpace(newValue)) {
if (!string.IsNullOrWhiteSpace(newValue))
bitmap = await AsyncImageLoader.ProvideImageAsync(newValue!);
}

if (bitmap == null && GetFallbackImage(imageBrush) is Bitmap fallback)
bitmap = fallback;
}
catch (Exception e) {
Logger?.Log("ImageBrushLoader", "ImageBrushLoader image resolution failed: {0}", e);
Expand All @@ -42,6 +44,32 @@ private static async void OnSourceChanged(ImageBrush imageBrush, AvaloniaPropert
public static readonly AttachedProperty<string?> SourceProperty =
AvaloniaProperty.RegisterAttached<ImageBrush, string?>("Source", typeof(ImageLoader));

/// <summary>
/// Attached property that provides a fallback <see cref="Bitmap"/> to use when <see cref="SourceProperty"/> is null or empty.
/// </summary>
public static readonly AttachedProperty<Bitmap?> FallbackImageProperty =
AvaloniaProperty.RegisterAttached<ImageBrush, Bitmap?>("FallbackImage", typeof(Bitmap));

/// <summary>
/// Gets the fallback <see cref="Bitmap"/> attached to the specified <see cref="ImageBrush"/>.
/// Returns <c>null</c> if no fallback image has been set.
/// </summary>
/// <param name="element">The <see cref="ImageBrush"/> to read the fallback image from.</param>
/// <returns>The fallback <see cref="Bitmap"/>, or <c>null</c> if none is set.</returns>
public static Bitmap? GetFallbackImage(ImageBrush element) {
return element.GetValue(FallbackImageProperty);
}

/// <summary>
/// Sets the fallback <see cref="Bitmap"/> on the specified <see cref="ImageBrush"/>.
/// The fallback image is used when the <see cref="SourceProperty"/> value is null or empty.
/// </summary>
/// <param name="element">The <see cref="ImageBrush"/> to set the fallback image on.</param>
/// <param name="value">The <see cref="Bitmap"/> to use as the fallback</param>
public static void SetFallbackImage(ImageBrush element, Bitmap? value) {
element.SetValue(FallbackImageProperty, value);
}

public static string? GetSource(ImageBrush element) {
return element.GetValue(SourceProperty);
}
Expand Down