private void ClosePopup()
{
if (null != _backgroundResizeStoryboard)
{
// Swap all the From/To values to reverse the animation
foreach (DoubleAnimation animation in _backgroundResizeStoryboard.Children)
{
double temp = animation.From.Value;
animation.From = animation.To;
animation.To = temp;
}
// Capture member variables for delegate closure
Popup popup = _popup;
Panel overlay = _overlay;
_backgroundResizeStoryboard.Completed += delegate
{
// Clear/close popup and overlay
if (null != popup)
{
popup.IsOpen = false;
popup.Child = null;
}
if (null != overlay)
{
overlay.Children.Clear();
}
};
// Begin the reverse animation
_backgroundResizeStoryboard.Begin();
// Reset member variables
_backgroundResizeStoryboard = null;
_popup = null;
_overlay = null;
}
else
{
if (null != _popup)
{
_popup.IsOpen = false;
_popup.Child = null;
_popup = null;
}
if (null != _overlay)
{
_overlay.Children.Clear();
_overlay = null;
}
}
SizeChanged -= OnContextMenuOrRootVisualSizeChanged;
if (null != _rootVisual)
{
_rootVisual.SizeChanged -= OnContextMenuOrRootVisualSizeChanged;
}
// Remove Click handler for ApplicationBar Buttons
foreach (ApplicationBarIconButton button in _applicationBarIconButtons)
{
button.Click -= OnEventThatClosesContextMenu;
}
_applicationBarIconButtons.Clear();
// Remove BackKeyPress handler from page
if (_page != null)
{
_page.BackKeyPress -= OnPageBackKeyPress;
ClearValue(ApplicationBarMirrorProperty);
_page = null;
}
// Update IsOpen
_settingIsOpen = true;
IsOpen = false;
_settingIsOpen = false;
OnClosed(new RoutedEventArgs());
}
}