From 0e767fd0cc9e745b41136673d122a2e50b641b02 Mon Sep 17 00:00:00 2001 From: Nikola Metulev Date: Tue, 30 May 2017 20:59:52 -0700 Subject: [PATCH 1/2] fixed loaded bug in masterdetailsview --- .../MasterDetailsView/MasterDetailsView.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs index 1d13c825076..09aea9172e0 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs @@ -50,6 +50,7 @@ public partial class MasterDetailsView : ItemsControl private VisualStateGroup _stateGroup; private VisualState _narrowState; private Frame _frame; + private bool _loaded = false; /// /// Initializes a new instance of the class. @@ -77,6 +78,17 @@ protected override void OnApplyTemplate() SetMasterHeaderVisibility(); OnDetailsCommandBarChanged(); OnMasterCommandBarChanged(); + + if (_loaded && _stateGroup == null) + { + _stateGroup = (VisualStateGroup)GetTemplateChild(WidthStates); + if (_stateGroup != null) + { + _stateGroup.CurrentStateChanged += OnVisualStateChanged; + _narrowState = GetTemplateChild(NarrowState) as VisualState; + UpdateView(true); + } + } } /// @@ -156,11 +168,14 @@ private void OnLoaded(object sender, RoutedEventArgs e) } _stateGroup = (VisualStateGroup)GetTemplateChild(WidthStates); - _stateGroup.CurrentStateChanged += OnVisualStateChanged; - - _narrowState = GetTemplateChild(NarrowState) as VisualState; + if (_stateGroup != null) + { + _stateGroup.CurrentStateChanged += OnVisualStateChanged; + _narrowState = GetTemplateChild(NarrowState) as VisualState; + UpdateView(true); + } - UpdateView(true); + _loaded = true; } private void OnUnloaded(object sender, RoutedEventArgs e) From 00245b7cf182a57435b88a880b6e235493d83fc4 Mon Sep 17 00:00:00 2001 From: Nikola Metulev Date: Tue, 30 May 2017 22:03:39 -0700 Subject: [PATCH 2/2] unsubscribed event --- .../MasterDetailsView/MasterDetailsView.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs b/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs index 09aea9172e0..f3c98ee4b11 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/MasterDetailsView/MasterDetailsView.cs @@ -189,6 +189,12 @@ private void OnUnloaded(object sender, RoutedEventArgs e) frame.Navigating -= OnFrameNavigating; } } + + if (_stateGroup != null) + { + _stateGroup.CurrentStateChanged -= OnVisualStateChanged; + _stateGroup = null; + } } ///