HelperTool


前言

提示:XML读写配置和PropertyGrid显示


一、UserControl设置

自定义控件如下:
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

        private int id = 1;

        public int ID
        {
            get { return this.id; }
            set
            {
                this.id = value;
                this.propertyGrid1.SelectedObject = SettingManager.GetInstance().FindSettingById(id);
                this.toolStripLabel1.Text = SettingManager.GetInstance().FindSettingById(id).Name;
            }
        }

控件的保存数据

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //if (//当前权限是否是工程师模式)
            //{
            // return;
            //}

           ;
            if (MessageBox.Show(this,"确认保存["+SettingManager.GetInstance().FindSettingById(ID).Name+"]?","询问",MessageBoxButtons.OKCancel)==DialogResult.OK)
            {
                SettingManager.GetInstance().FindSettingById(ID).SaveSetting();
            }
        }

二、使用步骤

1.写XSetting基类

代码如下(示例):

        protected ConcurrentDictionary<string, string> settingDic = new ConcurrentDictionary<string, string>();
        protected string path;
        protected string root;
        public string Name;
 public void SetPathAndRoot(string path, string root )
        {
            this.path = path;
            this.root = root;
          
        }
       
public int LoadSetting()
       {

           try
           {
               XmlDocument document = new XmlDocument();
               document.Load(path);
               XmlNode xmlNodeParent = document.SelectSingleNode(root);
               if (xmlNodeParent==null)
               {
                   return 1;
               }
               XmlNodeList xmlNodeChildren = xmlNodeParent.ChildNodes;
               foreach (XmlElement item in xmlNodeChildren)
               {
                   settingDic.TryAdd(item.Name,item.InnerText );
               }

               return 0;
           }
           catch (Exception ex)
           {

               Trace.WriteLine(ex,this.GetType().ToString());
               return -1;
           }               
       }
        public int SaveSetting()
        {

            try
            {
                bool isContain = false;
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNode parent = doc.SelectSingleNode(root);
                XmlNodeList children = parent.ChildNodes;
                foreach (KeyValuePair<string,string> kvp in settingDic)
                {
                    isContain = false;
                    foreach (XmlElement xmlElement in children)
                    {
                        if (xmlElement.Name== kvp.Key)
                        {
                            xmlElement.InnerText = kvp.Value;
                            isContain = true;
                        }
                    }
                    if (isContain==false)
                    {
                        XmlElement newxml = doc.CreateElement(kvp.Key);
                        newxml.InnerText = kvp.Value;
                        parent.AppendChild(newxml);
                    }
                }
                doc.Save(path);

                return 0;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex,this.GetType().ToString());
                return -1;
            }     
        }



2.写类的管理类SettingManager

代码如下(示例):

        public SettingManager()
        {

        }
        private Dictionary<int, XSetting> dicSetMgr = new Dictionary<int, XSetting>();
  public void BindSetting(int setID,XSetting setting,string name)
        {
            if (dicSetMgr.ContainsKey(setID)==false)
            {
                dicSetMgr.Add(setID,setting);
                setting.Name = name;
            }
        
        }
        public XSetting FindSettingById(int id)
        {

            if (dicSetMgr.ContainsKey(id)==false)
            {
                return null;
            }

            return dicSetMgr[id];
        }

        public void LoadSettings()
        {
            foreach (XSetting item in dicSetMgr.Values)
            {
                item.LoadSetting();
            }
        
        
        
        }

3.写实际的保存类 CamSetting:XSetting

        public CamSetting()
        {


        }
        [Category("相机标定"), Description("单像素距离X")]
        public string  Pix_X
        {
            get { return settingDic["Pix_X"]; }
            set { settingDic["Pix_X"] = value; }
        }
     
        [Category("相机标定"),Description("单像素距离Y")]
        public string Pix_Y
        {
            get { return settingDic["Pix_Y"]; }
            set { settingDic["Pix_Y"] = value; }
        }


        public string Distance
        {
            get { return settingDic["Distance"]; }
            set { settingDic["Distance"] = value; }
        }
 [Category("机台速度")]
        public double XY_移动速度
        {
            get { return double.Parse(settingDic["LockXY_MoveVel"]); }
            set
            {
                if (value < 1 || value > 1001)
                    MessageBox.Show("XY_移动速度:1~1000", "参数设置超出范围", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    settingDic["LockXY_MoveVel"] = value.ToString();
            }
        }


实际使用

            CamSetting CamSetting = new CamSetting();
            CamSetting.SetPathAndRoot("CamXML.xml", "Setting");

            SettingManager.GetInstance().BindSetting(1, CamSetting, "相机设置");
            SettingManager.GetInstance().LoadSettings();
            this.settingGrid1.ID = 1;

效果展示

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值