Exemple #1
0
        /// <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();
            }
        }
        /// <summary>
        /// Overrides Invoke method
        /// </summary>
        /// <param name="parameter"></param>
        protected override void Invoke(object parameter)
        {
            if (PreCheckFuncBeforeOpen != null && !PreCheckFuncBeforeOpen())
            {
                return;
            }

            try
            {
                var windowObj = Activator.CreateInstance(WindowType);

                var window = windowObj as Window;

                var closeEventHanlder = new EventHandler((s, e) =>
                {
                    // first execute CommandAfterClose command, then invoke MethodAfterClose method (if they are set)
                    CommandAfterClose?.Execute(null);

                    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 && MethodOfTargetObject is FrameworkElement)
                        {
                            var dataContext = (MethodOfTargetObject as FrameworkElement).DataContext;
                            if (dataContext != null)
                            {
                                method = dataContext.GetType().GetMethod(MethodAfterClose, BindingFlags.Public | BindingFlags.Instance);
                                method?.Invoke(dataContext, null);
                            }
                        }
                        else
                        {
                            method = MethodOfTargetObject?.GetType().GetMethod(MethodAfterClose, BindingFlags.Public | BindingFlags.Instance);
                            method?.Invoke(MethodOfTargetObject, null);
                        }
                    }
                });

                window.Closed += closeEventHanlder;

                if (window.DataContext != null && window.DataContext is ViewModelRootBase)
                {
                    // set the data to viewmodel
                    (window.DataContext as ViewModelRootBase).Data = Parameter;
                }
                else
                {
                    window.Tag = Parameter;
                }

                if (IsModal)
                {
                    window.ShowDialog();
                }
                else
                {
                    window.Show();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
        }
Exemple #3
0
        protected override async void Invoke(object parameter)
        {
            try
            {
                if (IsSingleton)
                {
                    var targetWindow = Application.Current.Windows.OfType <Window>().FirstOrDefault(m => m.GetType() == WindowType);
                    if (targetWindow != null)
                    {
                        targetWindow.Activate();
                        return;
                    }
                }

                if (this.ChenckingFuncBeforeOpenning != null)
                {
                    if (!ChenckingFuncBeforeOpenning())
                    {
                        return;
                    }
                }

                if (CommandBeforeOpen != null)
                {
                    CommandBeforeOpen.Execute(null);
                }

                if (!string.IsNullOrWhiteSpace(MethodBeforeOpen))
                {
                    MethodInfo method = null;
                    Task       task   = null;

                    // 如果没有指定 MethodOfTargetObject,则默认为 AssociatedObject 的 DataContext (通常是 ViewModel)
                    if (MethodOfTargetObject == null && MethodOfTargetObject is FrameworkElement)
                    {
                        var dataContext = (MethodOfTargetObject as FrameworkElement).DataContext;
                        if (dataContext != null)
                        {
                            method = dataContext.GetType().GetMethod(MethodBeforeOpen, BindingFlags.Public | BindingFlags.Instance);
                            task   = (Task)method?.Invoke(dataContext, null);
                        }
                    }
                    else
                    {
                        method = MethodOfTargetObject?.GetType().GetMethod(MethodBeforeOpen, BindingFlags.Public | BindingFlags.Instance);
                        task   = (Task)method?.Invoke(MethodOfTargetObject, null);
                    }

                    if (task != null)
                    {
                        await task;
                    }
                }
                if (WindowType == null)
                {
                    WindowType = ContainerExtension.GetWindowObject(WindowName);
                }

                var windowObj = Activator.CreateInstance(WindowType);

                var window = windowObj as Window;
                window.Closed += (s, e) =>
                {
                    // 获取 Return Value
                    object returnValue = null;

                    if (window.DataContext != null)
                    {
                        if (window.DataContext is IDialogResult dialogResult)
                        {
                            returnValue = dialogResult;
                        }
                    }

                    // 先调用 Command(如果设置了),再调用方法(如果同样设置了)
                    // TODO: 这里临时这样做,如果没有返回值,则在调用 After Close 系列方法时,传入 Parameter 参数
                    //if (returnValue == null)
                    //{
                    //    returnValue = Parameter;
                    //}
                    CommandAfterClose?.Execute(returnValue);

                    // 再调用方法
                    if (!string.IsNullOrWhiteSpace(MethodAfterClose))
                    {
                        MethodInfo method = null;

                        // 如果没有指定 MethodOfTargetObject,则默认为 AssociatedObject 的 DataContext (通常是 ViewModel)
                        if (MethodOfTargetObject == null && MethodOfTargetObject is FrameworkElement)
                        {
                            var dataContext = (MethodOfTargetObject 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);
                        }
                    }
                };

                if (window.DataContext != null && window.DataContext is IDialogResult)
                {
                    (window.DataContext as IDialogResult).Parameters = Parameter as IDialogParameters;
                }
                else
                {
                    window.Tag = Parameter;
                }

                if (IsModal)
                {
                    window.Owner = WindowOperation.GetCurrentActivatedWindow();
                    window.ShowDialog();
                }
                else
                {
                    window.Show();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #4
0
        private void WindowShow()
        {
            if (WindowType == null)
            {
                WindowType = ContainerExtension.GetWindowObject(WindowName);
            }

            var windowObj = Activator.CreateInstance(WindowType);

            var window = windowObj as Window;

            window.Closed += (s, e) =>
            {
                // 获取 Return Value
                object returnValue = null;

                if (window.DataContext != null)
                {
                    if (window.DataContext is IDialogResult dialogResult)
                    {
                        returnValue = dialogResult;
                    }
                }

                // 先调用 Command(如果设置了),再调用方法(如果同样设置了)
                // TODO: 这里临时这样做,如果没有返回值,则在调用 After Close 系列方法时,传入 Parameter 参数
                //if (returnValue == null)
                //{
                //    returnValue = Parameter;
                //}
                CommandAfterClose?.Execute(returnValue);

                // 再调用方法
                if (!string.IsNullOrWhiteSpace(MethodAfterClose))
                {
                    MethodInfo method = null;

                    // 如果没有指定 MethodOfTargetObject,则默认为 AssociatedObject 的 DataContext (通常是 ViewModel)
                    if (MethodOfTargetObject == null && MethodOfTargetObject is FrameworkElement)
                    {
                        var dataContext = (MethodOfTargetObject 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);
                    }
                }
            };

            if (window.DataContext != null && window.DataContext is IDialogResult)
            {
                (window.DataContext as IDialogResult).Parameters = Parameter as IDialogParameters;
            }
            else
            {
                window.Tag = Parameter;
            }
            if (IsHandleWindow)
            {
                if (IsModal)
                {
                    WindowHandleManagement.HandleShowDialogWindow(new IntPtr(1), window, true);
                }
                else
                {
                    WindowHandleManagement.HandleShowWindow(new IntPtr(1), window, true);
                }
            }
            else
            {
                window.Owner = WindowOperation.GetCurrentActivatedWindow();
                if (IsModal)
                {
                    window.ShowDialog();
                }
                else
                {
                    window.Show();
                }
            }
        }