public void ApplyKeyGesturesTest1() { // Todo: MockMe Type type = null; // TODO: Initialize to an appropriate value ICanInputBind inputBinder = null; // TODO: Initialize to an appropriate value object instance = null; // TODO: Initialize to an appropriate value CommandKeyGestureAttribute.ApplyKeyGestures(type, inputBinder, instance); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
/// <summary> /// Applies key gestures to properties marked with the CommandKeyGesture attribute. /// </summary> /// <param name="type">The type to search for CommandKeyGesture attributes on public /// properties with the ICommand type.</param> /// <param name="inputBinder">The target to bind the key to.</param> /// <param name="instance">The instance with the command to bind to.</param> public static void ApplyKeyGestures(Type type, ICanInputBind inputBinder, object instance) { //var properties = type.GetMembers(); // get all public properties of the parameter type var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var item in propertyInfos) { if (!item.PropertyType.IsAssignableFrom(typeof(ICommand))) { // this is no ICommand, so move to the next. continue; } // search for the custom attribute. var attributes = item.GetCustomAttributes(typeof(CommandKeyGestureAttribute), true); var command = item.GetValue(instance, null) as ICommand; if (command == null) { // the ICommand is not set, move to the next. continue; } foreach (var attribute in attributes.OfType <CommandKeyGestureAttribute>()) { if (!string.IsNullOrEmpty(attribute.Target)) { var field = instance.GetType().GetField( attribute.Target, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (field != null) { // When there is a target for this binder, then // skip all fields that are not stored in the attributes target. var fieldValue = field.GetValue(instance); if (inputBinder is InputBindWrapper) { var ibunwrap = (InputBindWrapper)inputBinder; if (!ReferenceEquals(fieldValue, ibunwrap.wrapTarget)) { continue; } } else { if (!ReferenceEquals(fieldValue, inputBinder)) { continue; } } } } // create a key binding and attach it to the window, control, etc. var keybinding = new KeyBinding(command, attribute.Key); if (command is RelayCommand) { ((RelayCommand)command).InputGestures.Add(attribute.Key); } inputBinder.InputBindings.Add(keybinding); } } }
/// <summary> /// Applies key gestures to properties marked with the CommandKeyGesture attribute. /// </summary> /// <param name="type">The type to search for CommandKeyGesture attributes on public /// properties with the ICommand type.</param> /// <param name="inputBinder">The target to bind the key to.</param> /// <param name="instance">The instance with the command to bind to.</param> public static void ApplyKeyGestures(Type type, ICanInputBind inputBinder, object instance) { //var properties = type.GetMembers(); // get all public properties of the parameter type var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var item in propertyInfos) { if (!item.PropertyType.IsAssignableFrom(typeof(ICommand))) { // this is no ICommand, so move to the next. continue; } // search for the custom attribute. var attributes = item.GetCustomAttributes(typeof(CommandKeyGestureAttribute), true); var command = item.GetValue(instance, null) as ICommand; if (command == null) { // the ICommand is not set, move to the next. continue; } foreach (var attribute in attributes.OfType<CommandKeyGestureAttribute>()) { if (!string.IsNullOrEmpty(attribute.Target)) { var field = instance.GetType().GetField( attribute.Target, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (field != null) { // When there is a target for this binder, then // skip all fields that are not stored in the attributes target. var fieldValue = field.GetValue(instance); if (inputBinder is InputBindWrapper) { var ibunwrap = (InputBindWrapper)inputBinder; if (!ReferenceEquals(fieldValue, ibunwrap.wrapTarget)) { continue; } } else { if (!ReferenceEquals(fieldValue, inputBinder)) { continue; } } } } // create a key binding and attach it to the window, control, etc. var keybinding = new KeyBinding(command, attribute.Key); if (command is RelayCommand) { ((RelayCommand)command).InputGestures.Add(attribute.Key); } inputBinder.InputBindings.Add(keybinding); } } }