WPF shortcut key unified management improvement article, wpf unified management

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.