protected override void Invoke(object parameter) { var args = parameter as InteractionRequestedEventArgs; if (args != null) { var notificationWithServicesAndState = args.Context as NotificationWithServicesAndState; if (notificationWithServicesAndState == null || notificationWithServicesAndState.AudioService == null || notificationWithServicesAndState.DictionaryService == null) { throw new ApplicationException(Resources.REQUIRED_SERVICES_NOT_PASSED_TO_MANAGEMENT_WINDOW); } var childWindow = new ManagementWindow(notificationWithServicesAndState.AudioService, notificationWithServicesAndState.DictionaryService); EventHandler closeHandler = null; closeHandler = (sender, e) => { childWindow.Closed -= closeHandler; args.Callback(); }; childWindow.Closed += closeHandler; var parentWindow = AssociatedObject != null ? AssociatedObject as Window ?? VisualAndLogicalTreeHelper.FindVisualParent <Window>(AssociatedObject) : null; bool parentWindowHadFocus = false; if (parentWindow != null && notificationWithServicesAndState.ModalWindow) { childWindow.Owner = parentWindow; //Setting the owner preserves the z-order of the parent and child windows when the focus is shifted back to the parent (otherwise the child popup will be hidden) parentWindowHadFocus = parentWindow.IsFocused; } if (notificationWithServicesAndState.ModalWindow) { Log.Info("Showing Management Window (modal)"); childWindow.Topmost = true; childWindow.ShowDialog(); } else { Log.Info("Showing Management Window (non-modal)"); childWindow.Show(); } if (parentWindow != null && notificationWithServicesAndState.ModalWindow) { if (parentWindowHadFocus) { Log.Debug("Parent Window was previously focussed - giving it focus again."); parentWindow.Focus(); } } } }
static void Init() { // Get existing open window or if none, make a new one: ManagementWindow window = (ManagementWindow)GetWindow(typeof(ManagementWindow)); window.Show(); }
protected override void Invoke(object parameter) { var args = parameter as InteractionRequestedEventArgs; if (args != null) { var notificationWithServices = args.Context as NotificationWithServices; if (notificationWithServices == null || notificationWithServices.AudioService == null || notificationWithServices.DictionaryService == null) { throw new ApplicationException("Audio and/or Dictionary service(s) were/was not supplied to the management window action."); } var childWindow = new ManagementWindow(notificationWithServices.AudioService, notificationWithServices.DictionaryService); EventHandler closeHandler = null; closeHandler = (sender, e) => { childWindow.Closed -= closeHandler; args.Callback(); }; childWindow.Closed += closeHandler; var parentWindow = AssociatedObject != null ? AssociatedObject as Window ?? VisualAndLogicalTreeHelper.FindVisualParent <Window>(AssociatedObject) : null; bool parentWindowHadFocus = false; bool parentWindowWasTopmost = false; if (parentWindow != null) { childWindow.Owner = parentWindow; //Setting the owner preserves the z-order of the parent and child windows when the focus is shifted back to the parent (otherwise the child popup will be hidden) parentWindowHadFocus = parentWindow.IsFocused; parentWindowWasTopmost = parentWindow.Topmost; parentWindow.Topmost = false; //Topmost must be revoked otherwise it cannot be reinstated correctly once the child window is closed } Log.Info("Showing Management window"); childWindow.ShowDialog(); if (parentWindow != null) { if (parentWindowHadFocus) { Log.Debug("Parent Window was previously focussed - giving it focus again."); parentWindow.Focus(); } if (parentWindowWasTopmost) { Log.Debug("Parent Window was previously top most - setting it back to top most window."); parentWindow.Topmost = true; } } } }