你的位置:首页 > ASP.net教程

[ASP.net教程]WPF 快捷键统一管理 改进篇


    感谢@zhoumy,提的建议。使用指定命名规则确实是个好方案!以下直接给出修改后的代码。如需查看前篇点击跳转

1、修改后的规则实体

  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、修改后的快捷键入口

 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 { }    }  }