WPF shortcut key unified management improvement article, wpf unified management
Thanks @ zhoumy for your suggestions. Using the specified naming rule is indeed a good solution! The modified code is provided below. To view the previous article, click to jump
1. modified rule entity
public class KeyboardShortcutsRule { public KeyboardShortcutsRule() { IsShowInHelp = true; IsShowInMainWindow = false; } public string Name { get; set; } public string Keys { get; set; } public bool IsShowInHelp { get; set; } public bool IsShowInMainWindow { get; set; } public List<Type> IgnoreWindow { get; set; } public List<Type> Effectivity { get; set; } private IKeyboardShortcutsCommand tmpCommand; public IKeyboardShortcutsCommand Command { get { if (tmpCommand == null) { string typeName = Name + "Command"; var assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); foreach (var item in assemblies) { var type = item.GetTypes().FirstOrDefault(o => o.ToString().EndsWith(typeName)); if (type != null && type.GetInterfaces().Contains(typeof(IKeyboardShortcutsCommand))) { tmpCommand = item.CreateInstance(type.ToString()) as IKeyboardShortcutsCommand; return tmpCommand; } } } return tmpCommand; } } }
2. Shortcut Key entry after modification
public class KeyboardShortcuts { private const string COFINGFILE = "KeyboardShortcutsRules.Config"; public KeyboardShortcuts() { Rules = new List<KeyboardShortcutsRule>(); LoadConfig(); } public List<KeyboardShortcutsRule> Rules { get; set; } public void ActiveKeysBindings(Window window) { foreach (var item in Rules) { if (!item.IgnoreWindow.Contains(window.GetType())) { if (item.Effectivity.Count == 0 || item.Effectivity.Contains(window.GetType()) && item.Command != null) { window.InputBindings.Add(new InputBinding(item.Command.GetCommand(), new KeyGestureConverter().ConvertFromString(item.Keys) as KeyGesture) { CommandParameter = window }); } } } } private static KeyboardShortcuts current = null; public static KeyboardShortcuts Current { get { if (current == null) { current = new KeyboardShortcuts(); } return current; } } public void LoadConfig() { try { XDocument doc = XDocument.Load(COFINGFILE); this.Rules = doc.Element("Settings").Elements("KeyboardShortcuts") .Select(o => new KeyboardShortcutsRule() { Name = o.Attribute("Name").Value, IsShowInHelp = o.Attribute("IsShowInHelp") != null ? Convert.ToBoolean(o.Attribute("IsShowInHelp").Value) : true, IsShowInMainWindow = o.Attribute("IsShowInMainWindow") != null ? Convert.ToBoolean(o.Attribute("IsShowInMainWindow").Value) : false, Keys = o.Attribute("Keys").Value, IgnoreWindow = new List<Type>().InitListType(o.Elements("Ignore")), Effectivity = new List<Type>().InitListType(o.Elements("Effect")) } ).ToList(); } catch { } } public void SaveConfig() { try { XDocument doc = new XDocument(); var element = new XElement("Settings"); foreach (var item in Rules) { var cmdType = item.Command.GetType(); XElement node = new XElement( "KeyboardShortcuts", new XAttribute("Name", item.Name), new XAttribute("Keys", item.Keys), new XAttribute("IsShowInHelp", item.IsShowInHelp), new XAttribute("IsShowInMainWindow", item.IsShowInMainWindow) ); foreach (var childItem in item.IgnoreWindow) { XElement ignoreList = new XElement("Ignore", childItem); node.Add(ignoreList); } foreach (var childItem in item.Effectivity) { XElement effectList = new XElement("Effect", childItem); node.Add(effectList); } element.Add(node); } doc.Add(element); doc.Save(COFINGFILE); } catch { } } }