Exemple #1
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 #2
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();
                }
            }
        }