diff --git a/Microsoft.Phone.Controls.Toolkit/BasePage/BasePage.cs b/Microsoft.Phone.Controls.Toolkit/BasePage/BasePage.cs index 2b23d85..ab252f7 100644 --- a/Microsoft.Phone.Controls.Toolkit/BasePage/BasePage.cs +++ b/Microsoft.Phone.Controls.Toolkit/BasePage/BasePage.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Windows; using System.Windows.Controls; +using System.Windows.Navigation; namespace Microsoft.Phone.Controls { @@ -21,41 +22,30 @@ public class BasePage : PhoneApplicationPage /// public BasePage() { - // When this page is part of the visual tree make the following changes: - // 1) Map orientation to visual state for the page - Loaded += (sender, e) => - { - StartLayoutUpdates(sender, e); - }; + } - // Undo the same changes when the page is no longer visible - Unloaded += (sender, e) => + /// + /// Called when a page becomes the active page in a frame. + /// + /// An object that contains the event data. + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + + if (_layoutAwareControls == null) { - StopLayoutUpdates(sender, e); - }; + StartLayoutUpdates(this); + } } #region Visual state switching /// - /// Invoked as an event handler, typically on the - /// event of a within the page, to indicate that the sender should - /// start receiving visual state management changes that correspond to orientation changes. + /// Start layout updates for the specified control. /// - /// Instance of that supports visual state - /// management corresponding to orientations. - /// Event data that describes how the request was made. - /// The current view state will immediately be used to set the corresponding - /// visual state when layout updates are requested. A corresponding - /// event handler connected to - /// is strongly encouraged. Instances of - /// automatically invoke these handlers in their Loaded and - /// Unloaded events. - /// - /// - public void StartLayoutUpdates(object sender, RoutedEventArgs e) + /// + public void StartLayoutUpdates(Control control) { - var control = sender as Control; if (control == null) return; if (_layoutAwareControls == null) { @@ -75,19 +65,11 @@ private void OnOrientationChanged(object sender, OrientationChangedEventArgs e) } /// - /// Invoked as an event handler, typically on the - /// event of a , to indicate that the sender should start receiving - /// visual state management changes that correspond to orientation changes. + /// Stop layout updates for the specified control. /// - /// Instance of that supports visual state - /// management corresponding to orientations. - /// Event data that describes how the request was made. - /// The current orientation will immediately be used to set the corresponding - /// visual state when layout updates are requested. - /// - public void StopLayoutUpdates(object sender, RoutedEventArgs e) + /// + public void StopLayoutUpdates(Control control) { - var control = sender as Control; if (control == null || _layoutAwareControls == null) return; _layoutAwareControls.Remove(control); if (_layoutAwareControls.Count == 0) diff --git a/Microsoft.Phone.Controls.Toolkit/PageUI/PageUI.cs b/Microsoft.Phone.Controls.Toolkit/PageUI/PageUI.cs index 209b09e..9170b82 100644 --- a/Microsoft.Phone.Controls.Toolkit/PageUI/PageUI.cs +++ b/Microsoft.Phone.Controls.Toolkit/PageUI/PageUI.cs @@ -18,9 +18,6 @@ public class PageUI : ContentControl public PageUI() { DefaultStyleKey = typeof(PageUI); - - Loaded += OnLoaded; - Unloaded += OnUnloaded; } #region Title @@ -188,22 +185,16 @@ public override void OnApplyTemplate() { base.OnApplyTemplate(); - _parentPage = this.GetParentByType(); - } - - private void OnLoaded(object sender, RoutedEventArgs e) - { if (_parentPage != null) { - _parentPage.StartLayoutUpdates(this, e); + _parentPage.StopLayoutUpdates(this); } - } - private void OnUnloaded(object sender, RoutedEventArgs e) - { + _parentPage = this.GetParentByType(); + if (_parentPage != null) { - _parentPage.StopLayoutUpdates(sender, e); + _parentPage.StartLayoutUpdates(this); } } }