Exemple #1
0
        public override void NavigateTo(IView view)
        {
            if (view == null)
            {
                throw new ArgumentNullException("view");
            }
            if (CurrentView == view)
            {
                return;
            }

            //if the view is already registered, then simply focus on it.
            for (int i = 0; i < RegisteredViews.Count; i++)
            {
                if (RegisteredViews[i] == view)
                {
                    int lastPos = Cursor;
                    Cursor = i;
                    ViewController.Focus(RegisteredViews[i]);
                    OnCurrentPositionChanged(lastPos);
                    return;
                }
            }

            //this is a new view; it's never been opened before.  If we are at the end of the row, then
            //simply add the view to the list.  Otherwise, we have to close all views in front of the current one.
            if (!this.CanGoForward())
            {
                RegisteredViews.Add(view);
                OnAddedView(view, this.CurrentPosition + 1);
                GoForward();
            }
            else
            {
                //Get the views that will be closed
                List <IView> forwards = new List <IView>(GetForwards());

                //ask the ViewController to close the views
                if (ViewController.CloseViewRange(forwards))
                {
                    //remove the closed views from the list
                    foreach (IView v in forwards)
                    {
                        RegisteredViews.Remove(v);
                    }
                    OnClosedForwards();

                    //add the newly added item, and focus it
                    RegisteredViews.Add(view);
                    OnAddedView(view, this.CurrentPosition + 1);
                    GoForward();
                }
            }
        }
Exemple #2
0
        public static void ProvideView(ViewModelBase vm, ViewModelBase parentVm = null)
        {
            IViewMap map = Maps.SingleOrDefault(x => x.ViewModelType == vm.GetType());

            if (map == null)
            {
                return;
            }

            var viewInstance = Activator.CreateInstance(map.ViewType) as Window;

            if (viewInstance == null)
            {
                throw new InvalidOperationException(string.Format("Can not create an instance of {0}", map.ViewType));
            }


            if (parentVm != null)
            {
                ViewInstance parent = RegisteredViews.SingleOrDefault(x => x.ViewModel == parentVm);

                if (parent != null)
                {
                    Window parentView = parent.View;

                    if (Application.Current.Windows.OfType <Window>().Any(x => Equals(x, parentView)))
                    {
                        viewInstance.Owner = parentView;
                    }
                }
            }

            viewInstance.DataContext = vm;

            RegisteredViews.Add(new ViewInstance(viewInstance, vm));

            viewInstance.Show();
        }