Example #1
0
        /// <summary>
        /// Returns a named view associated with the current action and passes a view model
        /// </summary>
        /// <param name="viewName">The name of the view that is to be returned</param>
        /// <param name="model">The model that is to be passed to the view</param>
        /// <param name="level">The level the view desires to be</param>
        /// <param name="forceNewShell">Indicates whether it is desired to launch this view in a new shell (may or may not be respected by each theme)</param>
        /// <returns>A view result</returns>
        /// <exception cref="CODE.Framework.Wpf.Mvvm.ViewNotFoundException"></exception>
        /// <example>
        /// public ActionResult ShowDetails()
        /// {
        ///    var model = new MyModel();
        ///    return Document("SomeView", model, ViewLevel.Popup);
        /// }
        ///   </example>
        protected virtual ViewResult View(string viewName, object model = null, ViewLevel level = ViewLevel.Normal, bool forceNewShell = false)
        {
            var result = new ViewResult {Model = model, ForceNewShell = forceNewShell};

            var locationsSearchedUnsuccessfully = new List<string>();
            if (!FindView(viewName, level, result, locationsSearchedUnsuccessfully))
            {
                var sb = new StringBuilder();
                foreach (var location in locationsSearchedUnsuccessfully) sb.AppendLine(location);
                throw new ViewNotFoundException(sb.ToString());
            }

            return result;
        }
Example #2
0
        /// <summary>Attempts to find the view using all currently registered view engines.</summary>
        /// <param name="viewName">Name of the view to be found</param>
        /// <param name="level">Display level of the view (such as top-level)</param>
        /// <param name="result">The view result object.</param>
        /// <param name="locationsSearchedUnsuccessfully">A list of locations the engines searched unsuccessfully (can be used by callers to display error messages and the like)</param>
        /// <returns>Success indicator (false if no view was found)</returns>
        private bool FindView(string viewName, ViewLevel level, ViewResult result, List<string> locationsSearchedUnsuccessfully = null)
        {
            var foundView = false;
            var controllerName = GetType().Name.ToLower();
            if (controllerName.ToLower().EndsWith("controller"))
                controllerName = controllerName.Substring(0, controllerName.Length - 10);

            foreach (var engine in RegisteredViewEngines)
            {
                var viewResult = engine.GetView(viewName, controllerName);
                if (viewResult.FoundView)
                {
                    result.View = viewResult.View;
                    result.ViewSource = viewResult.ViewSource;
                    result.ViewTitle = SimpleView.GetTitle(viewResult.View);
                    result.ViewIconResourceKey = SimpleView.GetIconResourceKey(viewResult.View);
                    result.View.DataContext = result.Model;
                    var haveViewInformation = result.Model as IHaveViewInformation;
                    if (haveViewInformation != null) haveViewInformation.AssociatedView = result.View;
                    result.ViewLevel = level;
                    foundView = true;
                    break;
                }
                if (locationsSearchedUnsuccessfully != null) locationsSearchedUnsuccessfully.AddRange(viewResult.LocationsSearched);
            }
            return foundView;
        }
Example #3
0
 private static void AfterViewOpened(ViewResult viewResult)
 {
     // This method executes all scheduled actions after a view has been opened, and removes the action from the stack.
     // This method is used internally by the controller to perform actions that need to be taken after the view has been completely loaded.
     while (AfterNextViewOpenActions.Count > 0)
     {
         var action = AfterNextViewOpenActions.Pop();
         action(viewResult);
     }
 }
Example #4
0
        /// <summary>This helper method finds the specified view which can often be useful in special cases, such as associating custom views with actions</summary>
        /// <param name="standardView">Standard view identifier</param>
        /// <param name="controllerType">Type of the controller (used as a context to find views)</param>
        /// <returns>Document as UIElement, or null if not found.</returns>
        public static FrameworkElement LoadView(StandardViews standardView, Type controllerType = null)
        {
            var viewName = "CODEFrameworkStandardView" + standardView;

            // If a controller type was specified, we try to use it, which provides a context to find views
            Controller controller;
            if (controllerType == null) controller = new Controller();
            else controller = Activator.CreateInstance(controllerType) as Controller;
            if (controller == null) controller = new Controller();
            var result = new ViewResult();
            return controller.FindView(viewName, ViewLevel.Normal, result) ? result.View : null;
        }
Example #5
0
        /// <summary>This helper method finds the specified view which can often be useful in special cases, such as associating custom views with actions</summary>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="controller">The controller name.</param>
        /// <returns>Document as UIElement, or null if not found.</returns>
        public static FrameworkElement LoadView(string viewName, string controller)
        {
            if (!controller.EndsWith("Controller")) controller += "Controller";

            if (_controllers == null) PopulateControllers();

            var controllerKey = controller.ToLower();
            if (_controllers != null && _controllers.ContainsKey(controllerKey))
            {
                var controllerInstance = _controllers[controllerKey].Instance;
                var result = new ViewResult();
                return controllerInstance.FindView(viewName, ViewLevel.Normal, result) ? result.View : null;
            }
            return null;
        }
Example #6
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;
        }
Example #7
0
        /// <summary>
        /// Opens a normal view in a separate window.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="viewResult">The view result.</param>
        private static void OpenNormalViewInWindow(RequestContext context, ViewResult viewResult)
        {
            var window = new Window
                {
                    Title = viewResult.ViewTitle,
                    SizeToContent = SizeToContent.WidthAndHeight,
                    Content = viewResult.View,
                    DataContext = viewResult.Model,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen
                };

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

            var simpleView = viewResult.View as SimpleView;
            if (simpleView != null)
                if (SimpleView.GetSizeStrategy(simpleView) == ViewSizeStrategies.UseMaximumSizeAvailable)
                    window.SizeToContent = SizeToContent.Manual;
            
            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-NormalLevelWindowStyle");
            if (viewResult.IsModal) window.ShowDialog();
            else window.Show();
        }