/// <summary> /// Open a window with the specific window info. /// </summary> /// <param name="openWindowInfo">The info of the window to be opened.</param> public virtual void ShowWindow(OpenWindowInfo openWindowInfo) { if (openWindowInfo == null || openWindowInfo.WindowType == null) { throw new ArgumentNullException(nameof(openWindowInfo), "WindowType cannot be null"); } Window window = null; object windowObj = null; try { windowObj = Activator.CreateInstance(openWindowInfo.WindowType); window = windowObj as Window; } catch (Exception ex) { throw new InvalidOperationException("Cannot create a window with the given type", ex); } if (openWindowInfo.Parameter != null && window.DataContext != null && window.DataContext is ViewModelRootBase) { (window.DataContext as ViewModelRootBase).Data = openWindowInfo.Parameter; } if (openWindowInfo.IsModal) { // set the owner window.Owner = AppWindow.GetCurrentActivatedWindow(); window.ShowDialog(); } else { window.Show(); } }
/// <summary> /// Overrides Invoke method /// </summary> /// <param name="parameter"></param> protected override void Invoke(object parameter) { if (ChenckingFuncBeforeOpenning != null && !ChenckingFuncBeforeOpenning()) { return; } if (CommandBeforeOpen != null) { CommandBeforeOpen.Execute(null); } Window window = null; object windowObj = null; try { windowObj = Activator.CreateInstance(WindowType); window = windowObj as Window; } catch (Exception ex) { throw new InvalidOperationException("Cannot create a window with the given type", ex); } var closeEventHanlder = new EventHandler((s, e) => { // check if need to process the return value object returnValue = window.TryGetReturnValue(); // first execute CommandAfterClose command, then invoke MethodAfterClose method (if they are set) CommandAfterClose?.Execute(returnValue); if (!string.IsNullOrWhiteSpace(MethodAfterClose)) { MethodInfo method = null; // if MethodOfTargetObject is not set, then the DataContext of AssociatedObject will be used as such a purpose if (MethodOfTargetObject == null) { var dataContext = (AssociatedObject as FrameworkElement).DataContext; if (dataContext != null) { method = dataContext.GetType().GetMethod(MethodAfterClose, BindingFlags.Public | BindingFlags.Instance); method?.Invoke(dataContext, returnValue != null ? new object[] { returnValue } : null); } } else { method = MethodOfTargetObject?.GetType().GetMethod(MethodAfterClose, BindingFlags.Public | BindingFlags.Instance); method?.Invoke(MethodOfTargetObject, returnValue != null ? new object[] { returnValue } : null); } } }); window.Closed -= closeEventHanlder; window.Closed += closeEventHanlder; if (window.DataContext != null && window.DataContext is ViewModelRootBase) { // set the data to viewmodel (window.DataContext as ViewModelRootBase).Data = Parameter; } if (IsModal) { // set the owner window.Owner = AppWindow.GetCurrentActivatedWindow(); window.ShowDialog(); } else { window.Show(); } }
private static void OnOpenWindowTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //// make it available for Button, Hyperlink, MenutItem //dynamic control = null; //switch (d.GetType().Name) //{ // case nameof(Button): // control = d as Button; // break; // case nameof(Hyperlink): // control = d as Hyperlink; // break; // case nameof(MenuItem): // control = d as MenuItem; // break; // default: // return; //} // make it avaible for those controls which has Click event var eventInfo = d.GetType().GetEvent("Click"); if (eventInfo == null) { throw new InvalidOperationException("The control attached does not have a event named Click"); } var type = GetOpenWindowType(d); if (type == null && type != typeof(Window)) { return; } Window window = null; var clickEventHandler = new RoutedEventHandler((s, arg) => { if (window == null) { window = Activator.CreateInstance(type) as Window; if (window == null) { throw new ArgumentException("cannot create a window by the target type"); } } if (GetParameter(d) != null) { if (window.DataContext != null && window.DataContext is ViewModelRootBase) { (window.DataContext as ViewModelRootBase).Data = GetParameter(d); } } var isModel = GetIsModal(d); window.Closed += (win, args) => { // try to get the return value from the window's view model var returnValueInfo = window.TryGetReturnValue(); var command = GetCommandAfterClose(d); if (command != null) { command.Execute(returnValueInfo.HasValue ? returnValueInfo.Value : null); } // set the object to null after it is closed window = null; }; if (isModel) { // set the owner window.Owner = AppWindow.GetCurrentActivatedWindow(); window.ShowDialog(); } else { window.Show(); } }); //control.Click -= clickEventHandler; //control.Click += clickEventHandler; eventInfo.RemoveEventHandler(d, clickEventHandler); eventInfo.AddEventHandler(d, clickEventHandler); }
private static void OnOpenWindowTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // make it available for Button, Hyperlink, MenutItem dynamic control = null; switch (d.GetType().Name) { case nameof(Button): control = d as Button; break; case nameof(Hyperlink): control = d as Hyperlink; break; case nameof(MenuItem): control = d as MenuItem; break; default: return; } var type = GetOpenWindowType(d); if (type == null && type != typeof(Window)) { return; } Window window = null; var clickEventHandler = new RoutedEventHandler((s, arg) => { if (window == null) { window = Activator.CreateInstance(type) as Window; if (window == null) { throw new ArgumentException("cannot create a window by the target type"); } } if (GetParameter(d) != null) { if (window.DataContext != null && window.DataContext is ViewModelRootBase) { (window.DataContext as ViewModelRootBase).Data = GetParameter(d); } //else //{ // window.Tag = GetParameter(d); //} } var isModel = GetIsModal(d); window.Closed += (win, closeArgs) => { var command = GetCommandAfterClose(d); if (command != null) { command.Execute(null); } // set the object to null after it is closed window = null; }; if (isModel) { // set the owner window.Owner = AppWindow.GetCurrentActivatedWindow(); window.ShowDialog(); } else { window.Show(); } }); control.Click -= clickEventHandler; control.Click += clickEventHandler; }