This project is read-only.

Navigation: Allow to show views more than once

Jan 20, 2010 at 10:15 AM

Hello,
i have a suggestion for the navigator. Why not use a naviagtion similar to the Formstack from OpenNetCf.ioc? Here is some sample code i've written. It's not heavily testet yet. It's benefit is that you can use the Controllers many times...

Navigator.cs
------------

private List<Controller> InstanceCache { get; set; }
        private List<Controller> Stack { get; set; }
        public int CurrentPosition { get; private set; }


        public void Navigate<T>(params object[] parameters) 
        {

            Controller view = InstanceCache.FirstOrDefault(v => v.GetType().Equals(typeof(T))) as Controller;

            if (view == null)
            {
                view = controllerProvider.GetController<T>();
                InstanceCache.Add(view);
            }

            // truncate at current position
            if (CurrentPosition < Count - 1)
            {
                Stack.RemoveRange(CurrentPosition + 1, Count - CurrentPosition - 1);
            }

            view.Initialize(parameters);

            Stack.Add(view);
            CurrentPosition = Count - 1;


            ShowHide((((CurrentPosition - 1 > 0) && (CurrentPosition - 1) <= Stack.Count) ? Stack[CurrentPosition - 1] : null), view);

        }
        
        public void GoBack()
        {
            if (CurrentPosition == 0)
            {
                Controller controller = (Stack.Count > 0) ? Stack[CurrentPosition] : null;

                if (controller != null)
                {
                    controller.View.Hide();
                    Stack.RemoveAt(CurrentPosition);
                }
                return;
            }

            CurrentPosition--;

            Controller currentController = Stack[CurrentPosition];

            ShowHide((((CurrentPosition + 1 >= 0) && (CurrentPosition + 1) <= Stack.Count) ? Stack[CurrentPosition + 1] : null), currentController);

        }
        
        public Controller GetCurrent()
        {
            if (Stack.Count > CurrentPosition - 1)
                return Stack[CurrentPosition];
            else
                return null;
        }
ApplicationManager.cs
---------------------
public static void Start()
{
     Navigator.Current.Navigate<LoginController>();
     Application.Run(Navigator.Current.GetCurrent().View as Form);
}
Kind regards svbirdy