|
1 |
| -using System; |
2 |
| -using System.Reactive.Linq; |
3 |
| -using AsyncImageLoader.Loaders; |
| 1 | +using AsyncImageLoader.Loaders; |
4 | 2 | using Avalonia;
|
5 | 3 | using Avalonia.Media;
|
6 | 4 |
|
7 | 5 | namespace AsyncImageLoader {
|
8 | 6 | public static class ImageBrushLoader {
|
9 | 7 | public static IAsyncImageLoader AsyncImageLoader { get; set; } = new RamCachedWebImageLoader();
|
10 | 8 | static ImageBrushLoader() {
|
11 |
| - SourceProperty.Changed |
12 |
| - .Where(args => args.IsEffectiveValueChange) |
13 |
| - .Subscribe(args => OnSourceChanged((ImageBrush)args.Sender, args.NewValue.Value)); |
| 9 | + SourceProperty.Changed.AddClassHandler<ImageBrush>(OnSourceChanged); |
14 | 10 | }
|
15 | 11 |
|
16 |
| - private static async void OnSourceChanged(ImageBrush sender, string? url) { |
17 |
| - SetIsLoading(sender, true); |
| 12 | + private static async void OnSourceChanged(ImageBrush imageBrush, AvaloniaPropertyChangedEventArgs args) { |
| 13 | + var (oldValue, newValue) = args.GetOldAndNewValue<string?>(); |
| 14 | + if (oldValue == newValue) |
| 15 | + return; |
| 16 | + |
| 17 | + SetIsLoading(imageBrush, true); |
18 | 18 |
|
19 |
| - var bitmap = url == null |
| 19 | + var bitmap = newValue == null |
20 | 20 | ? null
|
21 |
| - : await AsyncImageLoader.ProvideImageAsync(url); |
22 |
| - if (GetSource(sender) != url) return; |
23 |
| - sender.Source = bitmap; |
| 21 | + : await AsyncImageLoader.ProvideImageAsync(newValue); |
| 22 | + if (GetSource(imageBrush) != newValue) return; |
| 23 | + imageBrush.Source = bitmap; |
24 | 24 |
|
25 |
| - SetIsLoading(sender, false); |
| 25 | + SetIsLoading(imageBrush, false); |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | public static readonly AttachedProperty<string?> SourceProperty = AvaloniaProperty.RegisterAttached<ImageBrush, string?>("Source", typeof(ImageLoader));
|
|
0 commit comments