示例#1
0
        /// <summary>Returns a message box result with an attached model (and potentially an optional view)</summary>
        /// <param name="model">Message box specific view model</param>
        /// <param name="viewName">Name of the view (ignored if no custom view is found)</param>
        /// <returns>Message box result</returns>
        protected virtual MessageBoxResult MessageBox(MessageBoxViewModel model, string viewName)
        {
            var result = new MessageBoxResult {Model = model, ViewTitle = model.Caption};

            if (_messageBoxResultQueue == null || _messageBoxResultQueue.Count == 0)
                // We are attempting to find the view (and attach it to the result context), but if we can't find it, we are OK with it.
                FindView(viewName, ViewLevel.Popup, result);
            else
            {
                MessageBoxResultQueueItem nextResult;
                lock (_messageBoxResultQueue)
                    nextResult = _messageBoxResultQueue.Dequeue();
                model.Result = nextResult.Result;
                if (nextResult.CustomAction != null)
                    nextResult.CustomAction(model);
                if (model.OnComplete != null)
                    model.OnComplete(result);
            }

            return result;
        }
示例#2
0
        /// <summary>
        /// Opens the top level view in a separate window.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="messageBoxResult">The message box result.</param>
        /// <param name="viewResult">The view result.</param>
        /// <returns>True if successfully opened</returns>
        private bool OpenTopLevelView(RequestContext context, MessageBoxResult messageBoxResult, ViewResult viewResult)
        {
            if (messageBoxResult != null && string.IsNullOrEmpty(viewResult.ViewIconResourceKey))
                viewResult.ViewIconResourceKey = messageBoxResult.ModelMessageBox.IconResourceKey;

            //Brush iconBrush = Brushes.Transparent;
            //if (!string.IsNullOrEmpty(viewResult.ViewIconResourceKey))
            //    try
            //    {
            //        var resource = Application.Current.FindResource(viewResult.ViewIconResourceKey);
            //        if (resource != null)
            //            iconBrush = (Brush) resource;
            //    }
            //    catch
            //    {
            //        iconBrush = Brushes.Transparent;
            //    }

            // If we respect local views and the view is in fact a local view, and we have a normal view already open, then we open it in a local way only.
            if (viewResult.ViewScope == ViewScope.Local && HandleLocalViewsSpecial && SelectedNormalView > -1)
            {
                var selectedView = NormalViews[SelectedNormalView];
                if (selectedView == null) return false;
                selectedView.LocalViews.Add(viewResult);
                if (viewResult.MakeViewVisibleOnLaunch)
                    selectedView.SelectedLocalViewIndex = selectedView.LocalViews.Count - 1;
                return true;
            }

            //Need to make sure we do not open more than allowed - Popups should not close underlying views.
            if (viewResult.ViewLevel != ViewLevel.Popup && MaximumTopLevelViewCount > -1)
            {
                var inplaceTopLevelviews = TopLevelViews.Where(v => v.TopLevelWindow == null).ToList();
                while (inplaceTopLevelviews.Count + 1 > MaximumTopLevelViewCount)
                {
                    CloseViewForModel(inplaceTopLevelviews[0].Model);
                    inplaceTopLevelviews.RemoveAt(0);
                }
            }

            TopLevelViews.Add(viewResult);

            if (viewResult.MakeViewVisibleOnLaunch && !(TopLevelViewLaunchMode == ViewLaunchMode.Popup || (TopLevelViewLaunchMode == ViewLaunchMode.InPlaceExceptPopups && viewResult.ViewLevel == ViewLevel.Popup)))
            {
                SelectedTopLevelView = TopLevelViews.Count - 1;
                SelectedTopLevelViewResult = SelectedTopLevelView > -1 ? TopLevelViews[SelectedTopLevelView] : null;
                if (viewResult.View != null)
                    if (!FocusHelper.FocusFirstControlDelayed(viewResult.View))
                        FocusHelper.FocusDelayed(viewResult.View);
            }
            TopLevelViewCount = TopLevelViews.Count;

            if (TopLevelViewLaunchMode == ViewLaunchMode.Popup || (TopLevelViewLaunchMode == ViewLaunchMode.InPlaceExceptPopups && viewResult.ViewLevel == ViewLevel.Popup))
            {
                var window = new Window
                    {
                        Title = viewResult.ViewTitle,
                        Content = viewResult.View,
                        DataContext = viewResult.Model,
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        Owner = this
                    };

                window.SetBinding(TitleProperty, new Binding("ViewTitle") { Source = viewResult });

                // Setting the size strategy
                var strategy = SimpleView.GetSizeStrategy(viewResult.View);
                switch (strategy)
                {
                    case ViewSizeStrategies.UseMinimumSizeRequired:
                        window.SizeToContent = SizeToContent.WidthAndHeight;
                        break;
                    case ViewSizeStrategies.UseMaximumSizeAvailable:
                        window.SizeToContent = SizeToContent.Manual;
                        window.Height = SystemParameters.WorkArea.Height;
                        window.Width = SystemParameters.WorkArea.Width;
                        break;
                    case ViewSizeStrategies.UseSuggestedSize:
                        window.SizeToContent = SizeToContent.Manual;
                        window.Height = SimpleView.GetSuggestedHeight(viewResult.View);
                        window.Width = SimpleView.GetSuggestedWidth(viewResult.View);
                        break;
                }

                viewResult.TopLevelWindow = window;

                if (context.Result is MessageBoxResult) window.SetResourceReference(StyleProperty, "CODE.Framework.Wpf.Mvvm.Shell-TopLevelMessageBoxWindowStyle");
                else window.SetResourceReference(StyleProperty, "CODE.Framework.Wpf.Mvvm.Shell-TopLevelWindowStyle");

                if (viewResult.View != null)
                    foreach (InputBinding binding in viewResult.View.InputBindings)
                        window.InputBindings.Add(binding);

                if (!FocusHelper.FocusFirstControlDelayed(window))
                    FocusHelper.FocusDelayed(window);
                
                if (viewResult.IsModal) window.ShowDialog();
                else window.Show();
            }


            //if (iconBrush != null)
            //{
            //    try
            //    {
            //        // TODO: Implement the icon logic
            //        //var iconRect = new Canvas {Height = 96, Width = 96, Background = iconBrush};
            //        //window.Icon = iconRect.ToIconSource();
            //    }
            //    catch
            //    {
            //    }
            //}

            return true;
        }