204 lines
9.1 KiB
C#
204 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using DTS.Common.Base;
|
|
using Microsoft.Practices.Prism.Regions;
|
|
|
|
namespace DTS.Common
|
|
{
|
|
/// <summary>
|
|
/// Provides extention for the RegionManager .
|
|
/// </summary>
|
|
public static class RegionManagerExtensions
|
|
{
|
|
/// <summary>
|
|
/// Clears the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
public static void ClearRegion(this IRegionManager regionManager, string regionName)
|
|
{
|
|
regionManager.Regions[regionName].Views.ToList()
|
|
.ForEach(view => RemoveView(regionManager, view));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a list of views from the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="interfaceForView">Type of the views' interface.</param>
|
|
/// <returns>A list of views.</returns>
|
|
public static IList<object> GetViews(this IRegionManager regionManager, string regionName, Type interfaceForView)
|
|
{
|
|
var views = new List<object>();
|
|
|
|
regionManager.Regions[regionName].Views.ToList()
|
|
.ForEach(view =>
|
|
{
|
|
if (interfaceForView.IsInstanceOfType(view))
|
|
views.Add(view);
|
|
});
|
|
|
|
return views;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a View from the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="interfaceForView">Type of the View's interface.</param>
|
|
/// <returns>A View.</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates a View in the specified region in case View exists.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="viewType">Type of the View.</param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deactivates views in the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes views from the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates a single View in the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a View to the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="view">A View.</param>
|
|
public static void AddViewToRegion(this IRegionManager regionManager, string regionName, object view)
|
|
{
|
|
regionManager.Regions[regionName].Add(view);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a View to the specified region and activates it.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="view">A View.</param>
|
|
public static void AddViewToRegionActivate(this IRegionManager regionManager, string regionName, object view)
|
|
{
|
|
regionManager.Regions[regionName].Add(view);
|
|
regionManager.Regions[regionName].Activate(view);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a specified View from all regions.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="view">A View to be removed.</param>
|
|
public static void RemoveView(this IRegionManager regionManager, object view)
|
|
{
|
|
foreach (var region in regionManager.Regions)
|
|
{
|
|
if (region.Views.Contains(view))
|
|
region.Remove(view);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a specified View from the specified region.
|
|
/// </summary>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="regionName">The region name.</param>
|
|
/// <param name="view">A View to be removed.</param>
|
|
public static void RemoveView(this IRegionManager regionManager, string regionName, object view)
|
|
{
|
|
var region = regionManager.Regions[regionName];
|
|
region.Deactivate(view);
|
|
region.Remove(view);
|
|
}
|
|
}
|
|
}
|