public void CloseAllViewsInRegion(string regionName)
 {
     if (Regions.ContainsKey(regionName))
     {
         Dispatcher.Invoke((Action)(() =>
         {
             for (int i = 0; i < RegionHistory[regionName].Count; i++)
             {
                 ViewToViewModel vtVm = RegionHistory[regionName].ElementAt(i) as ViewToViewModel;
                 var control = vtVm.View;
                 var vievModel = control.DataContext as IBaseViewModel;
                 if (vievModel != null)
                 {
                     control.DataContext = null;
                     vievModel.Close();
                 }
             }
         }));
     }
 }
        public object NavigatBack(string regionName)
        {
            object returnModel = null;

            if (Regions.ContainsKey(regionName))
            {
                Dispatcher.Invoke((Action)(() =>
                {
                    var region = Regions[regionName];
                    int currentIndex = GetCurrentIndex(regionName);
                    if (currentIndex > 0)
                    {
                        var oldControl = (UserControl)region.Content;
                        if (oldControl != null)
                        {
                            var baseViewModel = oldControl.DataContext as IBaseViewModel;
                            if (baseViewModel != null)
                            {
                                oldControl.DataContext = null;
                                baseViewModel.Close();
                            }
                        }

                        try
                        {
                            if (!RegionHistory[regionName][currentIndex - 1].Region.IsVirtualRegion)
                            {
                                foreach (var childRegion in region.ChildRegions.Values)
                                {
                                    childRegion.Close();
                                }
                                region.Visibility = Visibility.Visible;

                                var view = RegionHistory[regionName][currentIndex - 1].View;
                                view.DataContext = null;
                                var model = Activator.CreateInstance(RegionHistory[regionName][currentIndex - 1].ViewModelType, RegionHistory[regionName][currentIndex - 1].Args);
                                view.DataContext = model;
                                returnModel = model;
                                if ((model is IBaseViewModel))
                                {
                                    ((IBaseViewModel)model).View = view;
                                }

                                RegionHistory[regionName][currentIndex - 1].Region.ActiveRegion.Content = view;
                                var vievToViewModel = new ViewToViewModel(view, (IBaseViewModel)model, null);
                                RegionToViewModel[regionName] = vievToViewModel;
                            }
                            else
                            {
                                region.Visibility = Visibility.Collapsed;

                                RegionHistory[regionName][currentIndex - 1].Region.Visibility = Visibility.Visible;
                                var view = RegionHistory[regionName][currentIndex - 1].View;
                                if (RegionHistory[regionName][currentIndex - 1].Region.Content != null)
                                {
                                    var baseViewModel = ((UserControl)RegionHistory[regionName][currentIndex - 1].Region.Content).DataContext as IBaseViewModel;
                                    if (baseViewModel != null)
                                    {
                                        baseViewModel.OnNavigationCompleted();
                                    }
                                    returnModel = baseViewModel;
                                    var vievToViewModel = new ViewToViewModel(view, baseViewModel, null);
                                    RegionToViewModel[regionName] = vievToViewModel;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.Message, ex);
                            Mediator.SendMessage(MsgTag.ShowFirstViewAndResetFilters, MsgTag.ShowFirstViewAndResetFilters);
                        }
                    }
                }));
            }
            else
            {
                throw new Exception("Invalid region name.");
            }
            return(returnModel);
        }
        public void NavigateForvard(string regionName)
        {
            if (Regions.ContainsKey(regionName))
            {
                Dispatcher.Invoke((Action)(() =>
                {
                    var region = Regions[regionName];
                    int currentIndex = 0;
                    for (int i = RegionHistory[regionName].Count - 1; i >= 0; i--)
                    {
                        var viewToVieModel = RegionHistory[regionName][i];
                        if (Equals(viewToVieModel.View, Regions[regionName].ActiveRegion.Content))
                        {
                            currentIndex = i;
                        }
                    }
                    if (currentIndex < RegionHistory[regionName].Count - 1)
                    {
                        var oldControl = (UserControl)region.Content;
                        if (oldControl != null)
                        {
                            var baseViewModel = oldControl.DataContext as IBaseViewModel;
                            if (baseViewModel != null)
                            {
                                oldControl.DataContext = null;
                                baseViewModel.Close();
                            }
                        }

                        if (!RegionHistory[regionName][currentIndex + 1].Region.IsVirtualRegion)
                        {
                            foreach (var childRegion in region.ChildRegions.Values)
                            {
                                childRegion.Close();
                            }
                            region.Visibility = Visibility.Visible;


                            var view = RegionHistory[regionName][currentIndex + 1].View;
                            view.DataContext = null;
                            var model = Activator.CreateInstance(RegionHistory[regionName][currentIndex + 1].ViewModelType, RegionHistory[regionName][currentIndex + 1].Args);
                            view.DataContext = model;
                            if ((model is IBaseViewModel))
                            {
                                ((IBaseViewModel)model).View = view;
                            }

                            RegionHistory[regionName][currentIndex + 1].Region.Content = view;
                            var vievToViewModel = new ViewToViewModel(view, (IBaseViewModel)model, null);
                            RegionToViewModel[regionName] = vievToViewModel;
                        }
                        else
                        {
                            region.Visibility = Visibility.Collapsed;

                            RegionHistory[regionName][currentIndex + 1].Region.Visibility = Visibility.Visible;
                            var view = RegionHistory[regionName][currentIndex + 1].View;
                            if (RegionHistory[regionName][currentIndex + 1].Region.Content != null)
                            {
                                var baseViewModel = ((UserControl)RegionHistory[regionName][currentIndex + 1].Region.Content).DataContext as IBaseViewModel;
                                if (baseViewModel != null)
                                {
                                    baseViewModel.OnNavigationCompleted();
                                }
                                var vievToViewModel = new ViewToViewModel(view, baseViewModel, null);
                                RegionToViewModel[regionName] = vievToViewModel;
                            }
                        }
                    }
                }));
            }
            else
            {
                throw new Exception("Invalid region name.");
            }
        }
        public T NavigateUsingViewModel <T>(string regionName, params object[] args)
        {
            if (ConfigurationManager.AppSettings["disable_views"] != null)
            {
                if (!Regions.ContainsKey(regionName))
                {
                    Regions[regionName]       = new MyContentRegion();
                    RegionHistory[regionName] = new List <ViewToViewModel>();
                }
            }
            T returnValue = default(T);

            if (Regions.ContainsKey(regionName))
            {
                Dispatcher.Invoke(() =>
                {
                    try
                    {
                        var region = Regions[regionName];

                        ClearForwardHistory(regionName);
                        var modelName = typeof(T).Name;



                        if (region.ChildRegions.ContainsKey(modelName))
                        {
                            foreach (var childRegion in region.ChildRegions.Values)
                            {
                                childRegion.Close();
                            }

                            region.Visibility = Visibility.Collapsed;
                            var oldControl    = (UserControl)region.Content;

                            if (oldControl != null)
                            {
                                var baseViewModel = oldControl.DataContext as IBaseViewModel;
                                if (baseViewModel != null)
                                {
                                    oldControl.DataContext = null;
                                    baseViewModel.Close();
                                }
                            }
                            foreach (var childRegion in region.ChildRegions.Values)
                            {
                                if (childRegion.VirtualRegionName == modelName)
                                {
                                    childRegion.Visibility = Visibility.Visible;
                                    if (childRegion.Content != null && ((UserControl)childRegion.Content).DataContext.GetType().Name == modelName)
                                    {
                                        var baseViewModel = ((UserControl)childRegion.Content).DataContext as IBaseViewModel;
                                        if (baseViewModel != null)
                                        {
                                            baseViewModel.OnNavigationCompleted();
                                            returnValue         = (T)baseViewModel;
                                            var vievToViewModel = new ViewToViewModel((UserControl)childRegion.Content, baseViewModel, childRegion, args);
                                            RegionHistory[regionName].Add(vievToViewModel);
                                            RegionToViewModel[regionName] = vievToViewModel;
                                        }
                                    }
                                    else
                                    {
                                        var control         = FindViewByViewModel <T>();
                                        var viewModel       = Activator.CreateInstance(typeof(T), args);
                                        childRegion.Content = control;
                                        control.DataContext = viewModel;
                                        returnValue         = (T)viewModel;
                                        if (viewModel is IBaseViewModel)
                                        {
                                            ((IBaseViewModel)viewModel).View = control;
                                        }
                                        var vievToViewModel = new ViewToViewModel(control, (IBaseViewModel)viewModel, childRegion, args);
                                        RegionHistory[regionName].Add(vievToViewModel);
                                        RegionToViewModel[regionName] = vievToViewModel;
                                    }
                                }
                                else
                                {
                                    childRegion.Close();
                                }
                            }
                        }
                        else
                        {
                            foreach (var childRegion in region.ChildRegions.Values)
                            {
                                childRegion.Close();
                            }
                            var oldControl = (UserControl)region.Content;

                            if (oldControl != null)
                            {
                                var baseViewModel = oldControl.DataContext as IBaseViewModel;
                                if (baseViewModel != null)
                                {
                                    oldControl.DataContext = null;
                                    baseViewModel.Close();
                                }
                            }
                            region.Visibility = Visibility.Visible;

                            var control         = FindViewByViewModel <T>();
                            var viewModel       = Activator.CreateInstance(typeof(T), args);
                            region.Content      = control;
                            control.DataContext = viewModel;
                            returnValue         = (T)viewModel;
                            if (viewModel is IBaseViewModel)
                            {
                                ((IBaseViewModel)viewModel).View = control;
                            }
                            var vievToViewModel = new ViewToViewModel(control, (IBaseViewModel)viewModel, region, args);
                            RegionHistory[regionName].Add(vievToViewModel);
                            RegionToViewModel[regionName] = vievToViewModel;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.Message, ex);
                    }
                });
            }
            else
            {
                throw new Exception("Invalid region name.");
            }

            return(returnValue);
        }