ImagePage.xaml.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.IO;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media.Imaging;
  6. namespace ControlCatalog.Pages
  7. {
  8. public class ImagePage : UserControl
  9. {
  10. private Image iconImage;
  11. public ImagePage()
  12. {
  13. this.InitializeComponent();
  14. }
  15. private void InitializeComponent()
  16. {
  17. AvaloniaXamlLoader.Load(this);
  18. iconImage = this.Get<Image>("Icon");
  19. }
  20. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  21. {
  22. base.OnAttachedToVisualTree(e);
  23. if (iconImage.Source == null)
  24. {
  25. var windowRoot = e.Root as Window;
  26. if (windowRoot != null)
  27. {
  28. using (var stream = new MemoryStream())
  29. {
  30. windowRoot.Icon.Save(stream);
  31. stream.Seek(0, SeekOrigin.Begin);
  32. iconImage.Source = new Bitmap(stream);
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }