Advertisement
celinedrules

WinUI3 ImageEx Control

Jan 5th, 2025 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | Source Code | 0 0
  1. using System;
  2. using Microsoft.UI.Xaml;
  3. using Microsoft.UI.Xaml.Controls;
  4. using Microsoft.UI.Xaml.Media;
  5. using Microsoft.UI.Xaml.Media.Imaging;
  6.  
  7. namespace PhotoOrganizer;
  8.  
  9. public class ImageEx : Control
  10. {
  11.     private Image? _mainImage;
  12.     private Image? _placeholderImage;
  13.    
  14.     public string PlaceholderSource
  15.     {
  16.         get => (string)GetValue(PlaceholderSourceProperty);
  17.         set => SetValue(PlaceholderSourceProperty, value);
  18.     }
  19.    
  20.     public ImageSource Source
  21.     {
  22.         get => (ImageSource)GetValue(SourceProperty);
  23.         set => SetValue(SourceProperty, value);
  24.     }
  25.  
  26.     public static readonly DependencyProperty SourceProperty =
  27.         DependencyProperty.Register(
  28.             nameof(Source),
  29.             typeof(ImageSource),
  30.             typeof(ImageEx),
  31.             new PropertyMetadata(null, OnSourceChanged));
  32.    
  33.     public static readonly DependencyProperty PlaceholderSourceProperty =
  34.         DependencyProperty.Register(nameof(PlaceholderSource), typeof(string), typeof(ImageEx), new PropertyMetadata(null));
  35.  
  36.     public ImageEx() => DefaultStyleKey = typeof(ImageEx);
  37.  
  38.     protected override void OnApplyTemplate()
  39.     {
  40.         base.OnApplyTemplate();
  41.    
  42.         _mainImage = GetTemplateChild("MainImage") as Image;
  43.         _placeholderImage = GetTemplateChild("PlaceholderImage") as Image;
  44.  
  45.         if (Source is BitmapImage bitmapImage)
  46.             SetImage(bitmapImage);
  47.         else
  48.             ShowPlaceholder();
  49.     }
  50.    
  51.     private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  52.     {
  53.         ImageEx control = (ImageEx)d;
  54.  
  55.         switch (e.NewValue)
  56.         {
  57.             case BitmapImage bitmapImage:
  58.                 control.SetImage(bitmapImage);
  59.                 break;
  60.             case string uriString when !string.IsNullOrEmpty(uriString):
  61.                 control.UpdateImage(uriString);
  62.                 break;
  63.             default:
  64.                 control.ShowPlaceholder();
  65.                 break;
  66.         }
  67.     }
  68.  
  69.     private void SetImage(BitmapImage image)
  70.     {
  71.         if (_mainImage == null)
  72.             return;
  73.        
  74.         _mainImage.Source = image;
  75.         _mainImage.Visibility = Visibility.Visible;
  76.  
  77.         if (_placeholderImage != null)
  78.             _placeholderImage.Visibility = Visibility.Collapsed;
  79.     }
  80.  
  81.  
  82.     private void UpdateImage(string source)
  83.     {
  84.         if (string.IsNullOrEmpty(source) || !Uri.IsWellFormedUriString(source, UriKind.RelativeOrAbsolute))
  85.             ShowPlaceholder();
  86.         else
  87.             ShowMainImage(source);
  88.     }
  89.  
  90.     private void ShowPlaceholder()
  91.     {
  92.         if (_placeholderImage != null)
  93.         {
  94.             _placeholderImage.Visibility = Visibility.Visible;
  95.  
  96.             Uri placeholderUri = PlaceholderSource.StartsWith("ms-appx://")
  97.                 ? new Uri(PlaceholderSource)
  98.                 : new Uri($"ms-appx:///{PlaceholderSource}");
  99.  
  100.             _placeholderImage.Source = new BitmapImage(placeholderUri);
  101.         }
  102.  
  103.         if (_mainImage != null)
  104.             _mainImage.Visibility = Visibility.Collapsed;
  105.     }
  106.    
  107.     private void ShowMainImage(string source)
  108.     {
  109.         if (_mainImage == null)
  110.             return;
  111.        
  112.         try
  113.         {
  114.             Uri imageUri = source.StartsWith("ms-appx://")
  115.                 ? new Uri(source)
  116.                 : new Uri($"ms-appx:///{source}");
  117.                
  118.             _mainImage.Source = new BitmapImage(imageUri);
  119.             _mainImage.Visibility = Visibility.Visible;
  120.  
  121.             if (_placeholderImage != null)
  122.                 _placeholderImage.Visibility = Visibility.Collapsed;
  123.         }
  124.         catch
  125.         {
  126.             ShowPlaceholder();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement