Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.UI.Xaml;
- using Microsoft.UI.Xaml.Controls;
- using Microsoft.UI.Xaml.Media;
- using Microsoft.UI.Xaml.Media.Imaging;
- namespace PhotoOrganizer;
- public class ImageEx : Control
- {
- private Image? _mainImage;
- private Image? _placeholderImage;
- public string PlaceholderSource
- {
- get => (string)GetValue(PlaceholderSourceProperty);
- set => SetValue(PlaceholderSourceProperty, value);
- }
- public ImageSource Source
- {
- get => (ImageSource)GetValue(SourceProperty);
- set => SetValue(SourceProperty, value);
- }
- public static readonly DependencyProperty SourceProperty =
- DependencyProperty.Register(
- nameof(Source),
- typeof(ImageSource),
- typeof(ImageEx),
- new PropertyMetadata(null, OnSourceChanged));
- public static readonly DependencyProperty PlaceholderSourceProperty =
- DependencyProperty.Register(nameof(PlaceholderSource), typeof(string), typeof(ImageEx), new PropertyMetadata(null));
- public ImageEx() => DefaultStyleKey = typeof(ImageEx);
- protected override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- _mainImage = GetTemplateChild("MainImage") as Image;
- _placeholderImage = GetTemplateChild("PlaceholderImage") as Image;
- if (Source is BitmapImage bitmapImage)
- SetImage(bitmapImage);
- else
- ShowPlaceholder();
- }
- private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- ImageEx control = (ImageEx)d;
- switch (e.NewValue)
- {
- case BitmapImage bitmapImage:
- control.SetImage(bitmapImage);
- break;
- case string uriString when !string.IsNullOrEmpty(uriString):
- control.UpdateImage(uriString);
- break;
- default:
- control.ShowPlaceholder();
- break;
- }
- }
- private void SetImage(BitmapImage image)
- {
- if (_mainImage == null)
- return;
- _mainImage.Source = image;
- _mainImage.Visibility = Visibility.Visible;
- if (_placeholderImage != null)
- _placeholderImage.Visibility = Visibility.Collapsed;
- }
- private void UpdateImage(string source)
- {
- if (string.IsNullOrEmpty(source) || !Uri.IsWellFormedUriString(source, UriKind.RelativeOrAbsolute))
- ShowPlaceholder();
- else
- ShowMainImage(source);
- }
- private void ShowPlaceholder()
- {
- if (_placeholderImage != null)
- {
- _placeholderImage.Visibility = Visibility.Visible;
- Uri placeholderUri = PlaceholderSource.StartsWith("ms-appx://")
- ? new Uri(PlaceholderSource)
- : new Uri($"ms-appx:///{PlaceholderSource}");
- _placeholderImage.Source = new BitmapImage(placeholderUri);
- }
- if (_mainImage != null)
- _mainImage.Visibility = Visibility.Collapsed;
- }
- private void ShowMainImage(string source)
- {
- if (_mainImage == null)
- return;
- try
- {
- Uri imageUri = source.StartsWith("ms-appx://")
- ? new Uri(source)
- : new Uri($"ms-appx:///{source}");
- _mainImage.Source = new BitmapImage(imageUri);
- _mainImage.Visibility = Visibility.Visible;
- if (_placeholderImage != null)
- _placeholderImage.Visibility = Visibility.Collapsed;
- }
- catch
- {
- ShowPlaceholder();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement