using System;
using System.Collections.Generic;
using System.Linq;
using DTS.Common.Base;
using Prism.Regions;
namespace DTS.Common
{
///
/// Provides extention for the RegionManager .
///
public static class RegionManagerExtensions
{
///
/// Clears the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
public static void ClearRegion(this IRegionManager regionManager, string regionName)
{
regionManager.Regions[regionName].Views.ToList()
.ForEach(view => RemoveView(regionManager, view));
}
///
/// Gets a list of views from the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// Type of the views' interface.
/// A list of views.
public static IList GetViews(this IRegionManager regionManager, string regionName, Type interfaceForView)
{
var views = new List();
regionManager.Regions[regionName].Views.ToList()
.ForEach(view =>
{
if (interfaceForView.IsInstanceOfType(view))
views.Add(view);
});
return views;
}
///
/// Gets a View from the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// Type of the View's interface.
/// A View.
public static object GetView(this IRegionManager regionManager, string regionName, Type interfaceForView)
{
object view = null;
regionManager.Regions[regionName].Views.ToList()
.ForEach(aView =>
{
if (interfaceForView.IsInstanceOfType(aView))
view = aView;
});
return view;
}
///
/// Activates a View in the specified region in case View exists.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// Type of the View.
///
public static bool ActivateViewIfExists(this IRegionManager regionManager, string regionName, Type viewType)
{
var returnValue = false;
var existingView = regionManager.GetView(regionName, viewType) as IBaseView;
if (existingView != null)
{
returnValue = true;
regionManager.ActivateSingleView(regionName, existingView);
}
return returnValue;
}
private static void ActivateSingleView(this IRegionManager regionManager, string regionName, IBaseView existingView)
{
regionManager.Regions[regionName].Activate(existingView);
var viewModel = existingView.DataContext as IBaseViewModel;
if (viewModel != null)
{
viewModel.Activated();
}
}
///
/// Deactivates views in the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
public static void DeactivateViews(this IRegionManager regionManager, string regionName)
{
var activeViews = regionManager.Regions[regionName].ActiveViews.ToList();
if (activeViews.Count > 0)
{
activeViews.ForEach(one => regionManager.Regions[regionName].Deactivate(one));
}
}
///
/// Removes views from the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
public static void RemoveViews(this IRegionManager regionManager, string regionName)
{
var activeViews = regionManager.Regions[regionName].ActiveViews.ToList();
if (activeViews.Count > 0)
{
activeViews.ForEach(one => regionManager.Regions[regionName].Remove(one));
}
}
///
/// Activates a single View in the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
public static void ActivateLastView(this IRegionManager regionManager, string regionName)
{
var activeViews = regionManager.Regions[regionName].ActiveViews.ToList();
if (activeViews.Count > 0)
{
var existingView = activeViews.Last() as IBaseView;
if (existingView != null)
{
regionManager.ActivateSingleView(regionName, existingView);
}
}
else
{
activeViews = regionManager.Regions[regionName].Views.ToList();
if (activeViews.Count > 0)
{
var existingView = activeViews.Last() as IBaseView;
if (existingView != null)
{
regionManager.ActivateSingleView(regionName, existingView);
}
}
}
}
///
/// Adds a View to the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// A View.
public static void AddViewToRegion(this IRegionManager regionManager, string regionName, object view)
{
regionManager.Regions[regionName].Add(view);
}
///
/// Adds a View to the specified region and activates it.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// A View.
public static void AddViewToRegionActivate(this IRegionManager regionManager, string regionName, object view)
{
regionManager.Regions[regionName].Add(view);
regionManager.Regions[regionName].Activate(view);
}
///
/// Removes a specified View from all regions.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// A View to be removed.
public static void RemoveView(this IRegionManager regionManager, object view)
{
foreach (var region in regionManager.Regions)
{
if (region.Views.Contains(view))
region.Remove(view);
}
}
///
/// Removes a specified View from the specified region.
///
/// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.
/// The region name.
/// A View to be removed.
public static void RemoveView(this IRegionManager regionManager, string regionName, object view)
{
var region = regionManager.Regions[regionName];
region.Deactivate(view);
region.Remove(view);
}
}
}