Ejemplo n.º 1
0
        /// <summary>
        /// This method is invoked when a view is opened
        /// </summary>
        /// <param name="context">Request context (contains information about the view)</param>
        /// <returns>
        /// True if handled successfully
        /// </returns>
        public bool OpenView(RequestContext context)
        {
            if (context.Result is StatusMessageResult)
            {
                return(HandleStatusMessage(context));
            }
            if (context.Result is NotificationMessageResult)
            {
                var result = context.Result as NotificationMessageResult;
                return(HandleNotificationMessage(context, result.Model.OverrideTimeout));
            }

            var viewResult = context.Result as ViewResult;

            if (viewResult != null && viewResult.ForceNewShell)
            {
                if ((viewResult.ViewLevel == ViewLevel.Normal && NormalViewCount > 0) || (viewResult.ViewLevel == ViewLevel.TopLevel && TopLevelViewCount > 0))
                {
                    // A new shell should be opened, so we try to create another shell and then hand off view opening duties to it, rather than handling it directly
                    var shellLauncher = Controller.GetShellLauncher() as WindowShellLauncher <Shell>;
                    if (shellLauncher != null)
                    {
                        if (shellLauncher.OpenAnotherShellInstance())
                        {
                            return(Current.OpenView(context));
                        }
                    }
                }
            }

            var messageBoxResult = context.Result as MessageBoxResult;

            if (messageBoxResult != null)
            {
                if (messageBoxResult.View == null)
                {
                    var messageBoxViewModel = messageBoxResult.Model as MessageBoxViewModel;
                    if (messageBoxViewModel != null)
                    {
                        var textBlock = new TextBlock {
                            TextWrapping = TextWrapping.Wrap, Text = messageBoxViewModel.Text
                        };
                        textBlock.SetResourceReference(StyleProperty, "CODE.Framework.Wpf.Mvvm.Shell-MessageBox-Text");

                        if (!string.IsNullOrEmpty(messageBoxViewModel.IconResourceKey))
                        {
                            var grid = new Grid();
                            grid.ColumnDefinitions.Clear();
                            grid.ColumnDefinitions.Add(new ColumnDefinition {
                                Width = GridLength.Auto
                            });
                            grid.ColumnDefinitions.Add(new ColumnDefinition {
                                Width = new GridLength(1, GridUnitType.Star)
                            });
                            var rect = new Rectangle {
                                Fill = messageBoxViewModel.IconBrush
                            };
                            rect.SetResourceReference(StyleProperty, "CODE.Framework.Wpf.Mvvm.Shell-MessageBox-Image");
                            grid.Children.Add(rect);
                            grid.Children.Add(textBlock);
                            Grid.SetColumn(textBlock, 1);
                            SimpleView.SetSizeStrategy(grid, ViewSizeStrategies.UseMinimumSizeRequired);
                            messageBoxResult.View = grid;
                        }
                        else
                        {
                            messageBoxResult.View = textBlock;
                        }
                    }
                }

                if (messageBoxResult.View != null)
                {
                    var messageBoxModelAction = messageBoxResult.Model as IHaveActions;
                    if (messageBoxModelAction != null)
                    {
                        messageBoxResult.View.InputBindings.Clear();
                        foreach (var action in messageBoxModelAction.Actions)
                        {
                            if (action.ShortcutKey != Key.None)
                            {
                                messageBoxResult.View.InputBindings.Add(new KeyBinding(action, action.ShortcutKey, action.ShortcutModifiers));
                            }
                        }
                    }
                }
            }

            if (viewResult != null && viewResult.View != null && !viewResult.IsPartial)
            {
                if (viewResult.ViewLevel == ViewLevel.Popup || viewResult.ViewLevel == ViewLevel.StandAlone || viewResult.ViewLevel == ViewLevel.TopLevel)
                {
                    return(OpenTopLevelView(context, messageBoxResult, viewResult));
                }

                // This is an normal (main) view

                // Need to make sure we do not open more than allowed
                if (MaximumNormalViewCount > -1)
                {
                    var inplaceNormalViews = NormalViews.Where(v => v.TopLevelWindow == null).ToList();
                    while (inplaceNormalViews.Count + 1 > MaximumNormalViewCount)
                    {
                        CloseViewForModel(inplaceNormalViews[0].Model);
                        inplaceNormalViews.RemoveAt(0);
                    }
                }

                NormalViews.Add(viewResult);
                if (viewResult.MakeViewVisibleOnLaunch)
                {
                    FocusManager.SetFocusedElement(this, viewResult.View);
                    SelectedNormalView       = NormalViews.Count - 1;
                    SelectedNormalViewResult = SelectedNormalView > -1 ? NormalViews[SelectedNormalView] : null;
                }
                NormalViewCount = NormalViews.Count;

                if (NormalViewLaunchMode == ViewLaunchMode.Popup)
                {
                    OpenNormalViewInWindow(context, viewResult);
                }

                return(true);
            }

            return(false);
        }