diff --git a/AsyncImageLoader.Avalonia/ImageBrushLoader.cs b/AsyncImageLoader.Avalonia/ImageBrushLoader.cs index 2ce55b9..9e7211c 100644 --- a/AsyncImageLoader.Avalonia/ImageBrushLoader.cs +++ b/AsyncImageLoader.Avalonia/ImageBrushLoader.cs @@ -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); @@ -42,6 +44,32 @@ private static async void OnSourceChanged(ImageBrush imageBrush, AvaloniaPropert public static readonly AttachedProperty SourceProperty = AvaloniaProperty.RegisterAttached("Source", typeof(ImageLoader)); + /// + /// Attached property that provides a fallback to use when is null or empty. + /// + public static readonly AttachedProperty FallbackImageProperty = + AvaloniaProperty.RegisterAttached("FallbackImage", typeof(Bitmap)); + + /// + /// Gets the fallback attached to the specified . + /// Returns null if no fallback image has been set. + /// + /// The to read the fallback image from. + /// The fallback , or null if none is set. + public static Bitmap? GetFallbackImage(ImageBrush element) { + return element.GetValue(FallbackImageProperty); + } + + /// + /// Sets the fallback on the specified . + /// The fallback image is used when the value is null or empty. + /// + /// The to set the fallback image on. + /// The to use as the fallback + public static void SetFallbackImage(ImageBrush element, Bitmap? value) { + element.SetValue(FallbackImageProperty, value); + } + public static string? GetSource(ImageBrush element) { return element.GetValue(SourceProperty); }