|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandManagerHelper.cs" company="JAGG">
// JAGG
// </copyright>
// <summary>
// This class is for command manipulation at the view model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MvvmSupport
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
/// <summary>
/// This class is for command manipulation at the view model.
/// </summary>
internal class CommandManagerHelper
{
#region Constructors
/// <summary>
/// Prevents a default instance of the <see cref="CommandManagerHelper"/> class from being created.
/// </summary>
private CommandManagerHelper()
{
}
#endregion Constructors
#region Methods
/// <summary>
/// Add handlers to command execution.
/// </summary>
/// <param name="handlers">Handlers to execute commands.</param>
internal static void AddHandlersToRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (var handler in handlers.Select(handlerRef => handlerRef.Target).OfType<EventHandler>())
{
CommandManager.RequerySuggested += handler;
}
}
}
/// <summary>
/// Add the handlers list to the lost of handlers of the same type.
/// </summary>
/// <param name="handlers">List that contain the handlers.</param>
/// <param name="handler">Event to add the handlers.</param>
internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler)
{
AddWeakReferenceHandler(ref handlers, handler, -1);
}
/// <summary>
/// Add a handler to the list of the same type.
/// </summary>
/// <param name="handlers">List with the handlers.</param>
/// <param name="handler">Event handler..</param>
/// <param name="defaultListSize">List size.</param>
internal static void AddWeakReferenceHandler(
ref List<WeakReference> handlers, EventHandler handler, int defaultListSize)
{
if (handlers == null)
{
handlers = defaultListSize > 0 ? new List<WeakReference>(defaultListSize) : new List<WeakReference>();
}
handlers.Add(new WeakReference(handler));
}
/// <summary>
/// Call the command handlers.
/// </summary>
/// <param name="handlers">Handlers list.</param>
internal static void CallWeakReferenceHandlers(List<WeakReference> handlers)
{
if (handlers != null)
{
var callees = new EventHandler[handlers.Count];
var count = 0;
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
var handler = reference.Target as EventHandler;
if (handler == null)
{
handlers.RemoveAt(i);
}
else
{
callees[count] = handler;
count++;
}
}
for (int i = 0; i < count; i++)
{
var handler = callees[i];
handler(null, EventArgs.Empty);
}
}
}
/// <summary>
/// Remove the handlers for the list.
/// </summary>
/// <param name="handlers">List with the hsandlers.</param>
internal static void RemoveHandlersFromRequerySuggested(List<WeakReference> handlers)
{
if (handlers != null)
{
foreach (var handler in handlers.Select(handlerRef => handlerRef.Target).OfType<EventHandler>())
{
CommandManager.RequerySuggested -= handler;
}
}
}
/// <summary>
/// Remove a specific handle for the list of command handlers.
/// </summary>
/// <param name="handlers">List of command handles.</param>
/// <param name="handler">Handler to be removed.</param>
internal static void RemoveWeakReferenceHandler(List<WeakReference> handlers, EventHandler handler)
{
if (handlers != null)
{
for (int i = handlers.Count - 1; i >= 0; i--)
{
WeakReference reference = handlers[i];
var existingHandler = reference.Target as EventHandler;
if ((existingHandler == null) || (existingHandler == handler))
{
// Remove all h.andlers added before this.
handlers.RemoveAt(i);
}
}
}
}
#endregion Methods
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and adviser, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxembourg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in Mexico working for Twenty Century Fox, and Mexico Stock Exchange (BMV). From 2013 to now in USA, Florida, First in FAME Inc. and now as Senior Software Engineer in Spirit Airlines.