using System.Collections.ObjectModel; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation.Peers; namespace Xamarin.Forms.Platform.UWP { public class PageRenderer : VisualElementRenderer { bool _disposed; bool _loaded; protected override AutomationPeer OnCreateAutomationPeer() { // Pages need an automation peer so we can interact with them in automated tests return new FrameworkElementAutomationPeer(this); } protected override void Dispose(bool disposing) { if (!disposing || _disposed) return; _disposed = true; if (Element != null) { ReadOnlyCollection children = ((IElementController)Element).LogicalChildren; for (var i = 0; i < children.Count; i++) { var visualChild = children[i] as VisualElement; visualChild?.Cleanup(); } Element?.SendDisappearing(); } base.Dispose(); } protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); e.OldElement?.SendDisappearing(); if (e.NewElement != null) { if (e.OldElement == null) { Loaded += OnLoaded; Tracker = new BackgroundTracker(BackgroundProperty); } if (!string.IsNullOrEmpty(Element.AutomationId)) { SetAutomationId(Element.AutomationId); } if (_loaded) e.NewElement.SendAppearing(); } } void OnLoaded(object sender, RoutedEventArgs args) { var carouselPage = Element?.Parent as CarouselPage; if (carouselPage != null && carouselPage.Children[0] != Element) { return; } _loaded = true; Unloaded += OnUnloaded; Element?.SendAppearing(); } void OnUnloaded(object sender, RoutedEventArgs args) { Unloaded -= OnUnloaded; _loaded = false; Element?.SendDisappearing(); } } }