void SetSecondaryCommandsToCloseWhenExecuted() { RevokeAndClear(m_secondaryButtonClickRevokerByElementMap); RevokeAndClear(m_secondaryToggleButtonCheckedRevokerByElementMap); RevokeAndClear(m_secondaryToggleButtonUncheckedRevokerByElementMap); RoutedEventHandler closeFlyoutFunc = delegate { Hide(); }; for (int i = 0; i < SecondaryCommands.Count; i++) { var element = SecondaryCommands[i]; var button = element as AppBarButton; var toggleButton = element as AppBarToggleButton; if (button != null) { m_secondaryButtonClickRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ButtonBase.ClickEvent, closeFlyoutFunc); } else if (toggleButton != null) { m_secondaryToggleButtonCheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.CheckedEvent, closeFlyoutFunc); m_secondaryToggleButtonUncheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.UncheckedEvent, closeFlyoutFunc); } } }
public CommandBarFlyout() { PrimaryCommands = new ObservableCollection <ICommandBarElement>(); SecondaryCommands = new ObservableCollection <ICommandBarElement>(); PrimaryCommands.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs args) => { if (m_commandBar != null) { SharedHelpers.ForwardCollectionChange((ObservableCollection <ICommandBarElement>)sender, m_commandBar.PrimaryCommands, args); } }; SecondaryCommands.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs args) => { var commandBar = m_commandBar; if (commandBar != null) { var source = (ObservableCollection <ICommandBarElement>)sender; SharedHelpers.ForwardCollectionChange(source, commandBar.SecondaryCommands, args); // We want to ensure that any interaction with secondary items causes the CommandBarFlyout // to close, so we'll attach a Click handler to any buttons and Checked/Unchecked handlers // to any toggle buttons that we get and close the flyout when they're invoked. // The only exception is buttons with flyouts - in that case, clicking on the button // will just open the flyout rather than executing an action, so we don't want that to // do anything. RoutedEventHandler closeFlyoutFunc = delegate { Hide(); }; // TODO switch (args.Action) { case NotifyCollectionChangedAction.Replace: { var element = (ICommandBarElement)args.NewItems[0]; var oldElement = (ICommandBarElement)args.OldItems[0]; var button = element as AppBarButton; var toggleButton = element as AppBarToggleButton; RevokeAndRemove(m_secondaryButtonClickRevokerByElementMap, oldElement); RevokeAndRemove(m_secondaryToggleButtonCheckedRevokerByElementMap, oldElement); RevokeAndRemove(m_secondaryToggleButtonUncheckedRevokerByElementMap, oldElement); if (button != null && button.Flyout == null) { m_secondaryButtonClickRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ButtonBase.ClickEvent, closeFlyoutFunc); RevokeAndRemove(m_secondaryToggleButtonCheckedRevokerByElementMap, element); RevokeAndRemove(m_secondaryToggleButtonUncheckedRevokerByElementMap, element); } else if (toggleButton != null) { RevokeAndRemove(m_secondaryButtonClickRevokerByElementMap, element); m_secondaryToggleButtonCheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.CheckedEvent, closeFlyoutFunc); m_secondaryToggleButtonUncheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.UncheckedEvent, closeFlyoutFunc); } else { RevokeAndRemove(m_secondaryButtonClickRevokerByElementMap, element); RevokeAndRemove(m_secondaryToggleButtonCheckedRevokerByElementMap, element); RevokeAndRemove(m_secondaryToggleButtonUncheckedRevokerByElementMap, element); } break; } case NotifyCollectionChangedAction.Add: { var element = (ICommandBarElement)args.NewItems[0]; var button = element as AppBarButton; var toggleButton = element as AppBarToggleButton; if (button != null && button.Flyout == null) { m_secondaryButtonClickRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ButtonBase.ClickEvent, closeFlyoutFunc); } else if (toggleButton != null) { m_secondaryToggleButtonCheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.CheckedEvent, closeFlyoutFunc); m_secondaryToggleButtonUncheckedRevokerByElementMap[element] = new RoutedEventHandlerRevoker( button, ToggleButton.UncheckedEvent, closeFlyoutFunc); } break; } case NotifyCollectionChangedAction.Remove: { var element = (ICommandBarElement)args.OldItems[0]; RevokeAndRemove(m_secondaryButtonClickRevokerByElementMap, element); RevokeAndRemove(m_secondaryToggleButtonCheckedRevokerByElementMap, element); RevokeAndRemove(m_secondaryToggleButtonUncheckedRevokerByElementMap, element); break; } case NotifyCollectionChangedAction.Move: break; case NotifyCollectionChangedAction.Reset: SetSecondaryCommandsToCloseWhenExecuted(); break; default: Debug.Assert(false); break; } } }; Opening += delegate { AreOpenCloseAnimationsEnabled = true; if (m_commandBar.HasOpenAnimation()) { InternalPopup.SuppressFadeAnimation = true; } else { InternalPopup.SuppressFadeAnimation = false; } if (ShowMode == FlyoutShowMode.Standard) { m_commandBar.IsOpen = true; } }; Opened += delegate { if (m_commandBar != null) { if (m_commandBar.HasOpenAnimation()) { m_commandBar.PlayOpenAnimation(); } else { AreOpenCloseAnimationsEnabled = false; } } }; Closing += delegate { var commandBar = m_commandBar; if (commandBar != null) { if (!m_isClosingAfterCloseAnimation && commandBar.HasCloseAnimation()) { //args.Cancel(true); commandBar.PlayCloseAnimation(() => { m_isClosingAfterCloseAnimation = true; Hide(); m_isClosingAfterCloseAnimation = false; }); commandBar.IsOpen = false; } //CommandBarFlyoutCommandBar.Closed will be called when //clicking the more (...) button, we clear the translations //here commandBar.ClearShadow(); } }; Closed += delegate { if (m_commandBar != null) { if (m_commandBar.IsOpen) { m_commandBar.IsOpen = false; } } }; }