CommandHelpers.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / MS / Internal / Commands / CommandHelpers.cs / 1 / CommandHelpers.cs

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Diagnostics; 
using System.Windows;
using System.Windows.Controls; 
using System.Windows.Input;
using System.Windows.Media;
using System.Security;
using System.Security.Permissions; 

namespace MS.Internal.Commands 
{ 
    internal static class CommandHelpers
    { 

        // Lots of specialized registration methods to avoid new'ing up more common stuff (like InputGesture's) at the callsite, as that's frequently
        // repeated and increases code size.  Do it once, here.
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, null); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    InputGesture inputGesture)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, inputGesture); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    Key key)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, new KeyGesture(key));
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    InputGesture inputGesture, InputGesture inputGesture2)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, inputGesture, inputGesture2); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, null); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, InputGesture inputGesture)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, inputGesture);
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, Key key)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, new KeyGesture(key)); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, InputGesture inputGesture, InputGesture inputGesture2)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, inputGesture, inputGesture2); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler,
                                                    InputGesture inputGesture, InputGesture inputGesture2, InputGesture inputGesture3, InputGesture inputGesture4) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler,
                                          inputGesture, inputGesture2, inputGesture3, inputGesture4);
        } 

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, Key key, ModifierKeys modifierKeys, 
                                                    ExecutedRoutedEventHandler executedRoutedEventHandler, CanExecuteRoutedEventHandler canExecuteRoutedEventHandler) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, new KeyGesture(key, modifierKeys)); 
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    string srid1, string srid2) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, 
                                                  KeyGesture.CreateFromResourceStrings(SR.Get(srid1), SR.Get(srid2))); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, string srid1, string srid2)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, 
                                                  KeyGesture.CreateFromResourceStrings(SR.Get(srid1), SR.Get(srid2)));
        } 
 
        // 'params' based method is private.  Call sites that use this bloat unwittingly due to implicit construction of the params array that goes into IL.
        private static void PrivateRegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                          CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params InputGesture[] inputGestures)
        {
            // Validate parameters
            Debug.Assert(controlType != null); 
            Debug.Assert(command != null);
            Debug.Assert(executedRoutedEventHandler != null); 
            // All other parameters may be null 

            // Create command link for this command 
            CommandManager.RegisterClassCommandBinding(controlType, new CommandBinding(command, executedRoutedEventHandler, canExecuteRoutedEventHandler));

            // Create additional input binding for this command
            if (inputGestures != null) 
            {
                for (int i = 0; i < inputGestures.Length; i++) 
                { 
                    CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i]));
                } 
            }

        }
 
        internal static bool CanExecuteCommandSource(ICommandSource commandSource)
        { 
            ICommand command = commandSource.Command; 
            if (command != null)
            { 
                object parameter = commandSource.CommandParameter;
                IInputElement target = commandSource.CommandTarget;

                RoutedCommand routed = command as RoutedCommand; 
                if (routed != null)
                { 
                    if (target == null) 
                    {
                        target = commandSource as IInputElement; 
                    }
                    return routed.CanExecute(parameter, target);
                }
                else 
                {
                    return command.CanExecute(parameter); 
                } 
            }
 
            return false;
        }

        ///  
        ///     Executes the command on the given command source.
        ///  
        ///  
        ///     Critical - calls critical function (ExecuteCommandSource)
        ///     TreatAsSafe - always passes in false for userInitiated, which is safe 
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        internal static void ExecuteCommandSource(ICommandSource commandSource)
        { 
            CriticalExecuteCommandSource(commandSource, false);
        } 
 
                /// 
        ///     Executes the command on the given command source. 
        /// 
        /// 
        /// Critical - sets the user initiated bit on a command, which is used
        ///            for security purposes later. It is important to validate 
        ///            the callers of this, and the implementation to make sure
        ///            that we only call MarkAsUserInitiated in the correct cases. 
        ///  
        [SecurityCritical]
        internal static void CriticalExecuteCommandSource(ICommandSource commandSource, bool userInitiated) 
        {
            ICommand command = commandSource.Command;
            if (command != null)
            { 
                object parameter = commandSource.CommandParameter;
                IInputElement target = commandSource.CommandTarget; 
 
                RoutedCommand routed = command as RoutedCommand;
                if (routed != null) 
                {
                    if (target == null)
                    {
                        target = commandSource as IInputElement; 
                    }
                    if (routed.CanExecute(parameter, target)) 
                    { 
                        routed.ExecuteCore(parameter, target, userInitiated);
                    } 
                }
                else if (command.CanExecute(parameter))
                {
                    command.Execute(parameter); 
                }
            } 
        } 
        // This allows a caller to override its ICommandSource values (used by Button and ScrollBar)
        internal static void ExecuteCommand(ICommand command, object parameter, IInputElement target) 
        {
            RoutedCommand routed = command as RoutedCommand;
            if (routed != null)
            { 
                if (routed.CanExecute(parameter, target))
                { 
                    routed.Execute(parameter, target); 
                }
            } 
            else if (command.CanExecute(parameter))
            {
                command.Execute(parameter);
            } 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Diagnostics; 
using System.Windows;
using System.Windows.Controls; 
using System.Windows.Input;
using System.Windows.Media;
using System.Security;
using System.Security.Permissions; 

namespace MS.Internal.Commands 
{ 
    internal static class CommandHelpers
    { 

        // Lots of specialized registration methods to avoid new'ing up more common stuff (like InputGesture's) at the callsite, as that's frequently
        // repeated and increases code size.  Do it once, here.
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, null); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    InputGesture inputGesture)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, inputGesture); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    Key key)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, new KeyGesture(key));
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    InputGesture inputGesture, InputGesture inputGesture2)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, inputGesture, inputGesture2); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, null); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, InputGesture inputGesture)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, inputGesture);
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, Key key)
        { 
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, new KeyGesture(key)); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, InputGesture inputGesture, InputGesture inputGesture2)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, inputGesture, inputGesture2); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler,
                                                    InputGesture inputGesture, InputGesture inputGesture2, InputGesture inputGesture3, InputGesture inputGesture4) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler,
                                          inputGesture, inputGesture2, inputGesture3, inputGesture4);
        } 

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, Key key, ModifierKeys modifierKeys, 
                                                    ExecutedRoutedEventHandler executedRoutedEventHandler, CanExecuteRoutedEventHandler canExecuteRoutedEventHandler) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, new KeyGesture(key, modifierKeys)); 
        }

        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    string srid1, string srid2) 
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, 
                                                  KeyGesture.CreateFromResourceStrings(SR.Get(srid1), SR.Get(srid2))); 
        }
 
        internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
                                                    CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, string srid1, string srid2)
        {
            PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, 
                                                  KeyGesture.CreateFromResourceStrings(SR.Get(srid1), SR.Get(srid2)));
        } 
 
        // 'params' based method is private.  Call sites that use this bloat unwittingly due to implicit construction of the params array that goes into IL.
        private static void PrivateRegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, 
                                                          CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params InputGesture[] inputGestures)
        {
            // Validate parameters
            Debug.Assert(controlType != null); 
            Debug.Assert(command != null);
            Debug.Assert(executedRoutedEventHandler != null); 
            // All other parameters may be null 

            // Create command link for this command 
            CommandManager.RegisterClassCommandBinding(controlType, new CommandBinding(command, executedRoutedEventHandler, canExecuteRoutedEventHandler));

            // Create additional input binding for this command
            if (inputGestures != null) 
            {
                for (int i = 0; i < inputGestures.Length; i++) 
                { 
                    CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i]));
                } 
            }

        }
 
        internal static bool CanExecuteCommandSource(ICommandSource commandSource)
        { 
            ICommand command = commandSource.Command; 
            if (command != null)
            { 
                object parameter = commandSource.CommandParameter;
                IInputElement target = commandSource.CommandTarget;

                RoutedCommand routed = command as RoutedCommand; 
                if (routed != null)
                { 
                    if (target == null) 
                    {
                        target = commandSource as IInputElement; 
                    }
                    return routed.CanExecute(parameter, target);
                }
                else 
                {
                    return command.CanExecute(parameter); 
                } 
            }
 
            return false;
        }

        ///  
        ///     Executes the command on the given command source.
        ///  
        ///  
        ///     Critical - calls critical function (ExecuteCommandSource)
        ///     TreatAsSafe - always passes in false for userInitiated, which is safe 
        /// 
        [SecurityCritical, SecurityTreatAsSafe]
        internal static void ExecuteCommandSource(ICommandSource commandSource)
        { 
            CriticalExecuteCommandSource(commandSource, false);
        } 
 
                /// 
        ///     Executes the command on the given command source. 
        /// 
        /// 
        /// Critical - sets the user initiated bit on a command, which is used
        ///            for security purposes later. It is important to validate 
        ///            the callers of this, and the implementation to make sure
        ///            that we only call MarkAsUserInitiated in the correct cases. 
        ///  
        [SecurityCritical]
        internal static void CriticalExecuteCommandSource(ICommandSource commandSource, bool userInitiated) 
        {
            ICommand command = commandSource.Command;
            if (command != null)
            { 
                object parameter = commandSource.CommandParameter;
                IInputElement target = commandSource.CommandTarget; 
 
                RoutedCommand routed = command as RoutedCommand;
                if (routed != null) 
                {
                    if (target == null)
                    {
                        target = commandSource as IInputElement; 
                    }
                    if (routed.CanExecute(parameter, target)) 
                    { 
                        routed.ExecuteCore(parameter, target, userInitiated);
                    } 
                }
                else if (command.CanExecute(parameter))
                {
                    command.Execute(parameter); 
                }
            } 
        } 
        // This allows a caller to override its ICommandSource values (used by Button and ScrollBar)
        internal static void ExecuteCommand(ICommand command, object parameter, IInputElement target) 
        {
            RoutedCommand routed = command as RoutedCommand;
            if (routed != null)
            { 
                if (routed.CanExecute(parameter, target))
                { 
                    routed.Execute(parameter, target); 
                }
            } 
            else if (command.CanExecute(parameter))
            {
                command.Execute(parameter);
            } 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK