码迷,mamicode.com
首页 > Windows程序 > 详细

WPF 快捷键统一管理 改进篇

时间:2015-05-19 18:11:12      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

    感谢@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 { }
        }


    }

 

WPF 快捷键统一管理 改进篇

标签:

原文地址:http://www.cnblogs.com/akong/p/4514843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!