public virtual void Raise <TViewModel>(string viewName, ViewModelInfo context, Action <TViewModel> onAvailable) where TViewModel : class { Raised?.Invoke(this, new ViewInteractionRequestEventArgs( viewName, context, view => { if (onAvailable != null) { var viewModel = ViewModelContainerHelper.GetViewModelAs <TViewModel>(view); onAvailable.Invoke(viewModel); } }) ); }
/// <summary> /// Invokes action /// </summary> /// <param name="parameter"></param> protected override void Invoke(object parameter) { if (string.IsNullOrEmpty(ViewName)) { return; } var args = parameter as InteractionRequestedEventArgs; if (args == null) { return; } // activate window if it has been created already if (SingleOnly) { var windowController = ServiceLocator.Current.GetInstance <IWindowController>(); if (windowController.GetWindow(ViewName) is T existingWindow) { Activate(existingWindow); return; } } var view = ServiceLocator.Current.GetInstance <IViewModelContainer>(ViewName); if (view == null) { return; } var window = CreateWindow(args.Context); if (Width != 0) { window.MinWidth = Width; window.Width = Width; } if (Height != 0) { window.MinHeight = Height; window.Height = Height; } if (WindowStyle != null) { window.Style = WindowStyle; } window.Content = view; var viewModel = ViewModelContainerHelper.GetViewModelAs <ICloseableViewModel>(view); if (viewModel != null) { OnClosing(window, viewModel); viewModel.Closed += (s, e) => CloseWindow(window); if (args.Context != null && viewModel is IConfigurableViewModel) { Configure(window, (IConfigurableViewModel)viewModel, args.Context.Content); } } var callback = args.Callback; OnClosed(window, callback, ViewName); if (StartupLocation == StartupLocationOption.CenterAssosiated && AssociatedObject != null) { // If we should center the pop-up over the parent window we subscribe to the SizeChanged event // so we can change its position after the dimensions are set. SizeChangedEventHandler sizeHandler = null; sizeHandler = (o, e) => { window.SizeChanged -= sizeHandler; try { var position = AssociatedObject.PointToScreen(new Point(0, 0)); var top = position.Y + ((AssociatedObject.ActualHeight - window.ActualHeight) / 2); var left = position.X + ((AssociatedObject.ActualWidth - window.ActualWidth) / 2); SetWindowPosition(window, left, top); } catch (Exception ex) { LogProvider.Log.Error(this, $"Window couldn't be centered", ex); } }; window.SizeChanged += sizeHandler; } Show(window); }