简体   繁体   English

有关将数据写入自定义部分的问题?

[英]A question about writing data to a custom section?

below is my class thanks to the article found at: 以下是我的课程,这要归功于在以下位置找到的文章:

URL: Derik Whittaker 网址: Derik Whittaker

My Code: 我的代码:

public class FavsSection : ConfigurationSection
    {
        public override bool IsReadOnly()
        {
            return base.IsReadOnly();
        }

        public FavsSection() // default Constructor.
        { }

        [ConfigurationProperty("Items", IsRequired=true)]
        public FavouritesCollection FavsItems
        {
            get 
            {
                return (FavouritesCollection)(base ["Items"]);
            }       
        }
    }

    [ConfigurationCollection(typeof(FavouriteElement))]
    public class FavouritesCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FavouriteElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FavouriteElement)(element)).ItemType;
        }

        public FavouriteElement this[int idx]
        {
            get
            {
                return (FavouriteElement)BaseGet(idx);
            }
        }

        public override bool IsReadOnly()
        {
            return base.IsReadOnly();
        }
    }

    public class FavouriteElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return ((string)(base["id"]));
            }
            set
            {
                base["id"] = value;
            }
        }

        [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Path
        {
            get
            {
                return ((string)(base["path"]));
            }
            set
            {
                base["path"] = value;
            }
        }
    }

My config file: 我的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="FavouritesMenu" type="MyFileExplorer.FavsSection, MyFileExplorer" />
  </configSections>
  <FavouritesMenu>
    <Items>
      <add id="1" path="c:\foo" />
      <add id="2" path="C:\foo1" />
    </Items>
  </FavouritesMenu>
</configuration>

As you can see I am trying to write data into my custom section called 'Favourites Menu'. 如您所见,我正在尝试将数据写入名为“收藏夹菜单”的自定义部分。 I think I have got the basic gist of the idea but I don;t see how to make my next step ... something got do with the 'IsReadOnly' method? 我想我已经有了这个想法的基本要点,但是我不知道如何进行下一步……“ IsReadOnly”方法有什么作用吗? Can someone please help me fill in the blanks? 有人可以帮我填补空白吗? Feel free to rename things to make it easier to read? 随意重命名事物以便于阅读? I thought I would make a half decent effort before I asked for help ... 我以为在寻求帮助之前会付出半点努力...

RESEARCH: StackOverFlow - SAME QUESTION! 研究: StackOverFlow-相同的问题!

---------- Got lost on Pike65's comment ... cannot write to collection because it is set to read only. ----------对Pike65的评论迷失了……无法将其写入集合,因为它被设置为只读。

I presume the collection needs setting to IsReadOnly false and some helper methods are needed to add data into the collection? 我假设集合需要将IsReadOnly设置为false,并且需要一些辅助方法才能将数据添加到集合中? This part is all alittle hazy to me ... 这部分对我来说有点朦胧...

Thanks for reading, Ibrar 感谢您的阅读,易卜拉欣

I have been doing some basic testing and to 'NO' surprise ... the above actually works. 我一直在进行一些基本测试,并且“不出意外”……以上方法确实有效。 You just need to make sure that when you want to pass data to your config section, then by default they are read only. 您只需要确保在要将数据传递到config部分时,默认情况下它们是只读的。 So you will need to override the 'isReadOnly()' method inorder for the above code to work. 因此,您需要重写'isReadOnly()'方法,以便上面的代码起作用。

So the solution is that the above peice of code, does work ... you just need to override an extra method to allow you to access the collection responsible for holding your element data and manipulate its contents via the properties you define in the class that extends or inherits from the Configuration Element class. 因此,解决方案是上面的代码确实有效……您只需要重写一个额外的方法,即可访问负责保存元素数据的集合,并通过您在该类中定义的属性来操纵其内容。扩展或继承自Configuration Element类。

UPDATE: 更新:

The above code sample I pasted in allows you to edit what already exists in the config file inside your custom section. 我粘贴的上述代码示例使您可以编辑自定义部分中配置文件中已经存在的内容。 In order to add a new item for example like the following: 为了添加一个新项目,例如如下所示:

            FavsSection favconfig = (FavsSection)config.GetSection("FavouritesMenu");

            ToolStripMenuItem menu = (ToolStripMenuItem)returnMenuComponents("favouritesToolStripMenuItem", form);

            ToolStripItemCollection items = menu.DropDownItems;

            for (int i = 0; i < items.Count; i++)
            {
                //favconfig.FavsItems[i].ID = i.ToString();
                //favconfig.FavsItems[i].Path = items[i].Text;

                favconfig.FavsItems[i] = new FavouriteElement()
                {
                    ID = i.ToString(),
                    Path = items[i].Text
                };
            }

As you can see above, I am physically adding a new 'FavouriteElement' object into the collection returned by the property 'favconfig.FavItems'. 如您在上面所看到的,我实际上是向属性“ favconfig.FavItems”返​​回的集合中添加一个新的“ FavouriteElement”对象。 In order to to do this, one property needs extending to support this. 为了做到这一点,一个属性需要扩展以支持这一点。

public FavouriteElement this[int idx]
{
    get
    {
        return (FavouriteElement)BaseGet(idx);
    }
    set
    {
      base.BaseAdd(value);
    }
}

This indexer or paramterful property as 'Jeffrey Richter' calls them needs to have it's 'Set' accessor implemented as shown above in the code snippet. 如“杰弗里·里希特”(Jeffrey Richter)所说的此索引器或参数属性,需要像上面的代码片段中所示实现其“设置”访问器。 I have pasted it in here as it did not take long to figure out and most of the code is changed using a template I have used from Derik Whittaker's Article. 我将其粘贴在这里,因为花了很长时间才弄清楚,并且大多数代码都是使用Derik Whittaker的文章中使用的模板进行更改的。 Hopefully this will alow other coders to implement something similar. 希望这将使其他编码人员能够实现类似的功能。

Another solution would be to simply rather than 'getting' the collection all the time that 'lassoes' together all my 'FavouriteElements', you could implement the 'set' accessor for the related property. 另一个解决方案是在将我的所有“ FavouriteElements”一起“套索”在一起的过程中,而不是简单地“获取”集合,您可以为相关属性实现“设置”访问器。 I have not tested this but I might be worth trying out. 我没有对此进行测试,但是我可能值得尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM