添加自定义类Settings.Settings集合 [英] Add a collection of a custom class to Settings.Settings

查看:266
本文介绍了添加自定义类Settings.Settings集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有一个宏大的一次尝试自定义类的自定义集合添加到我的WinForms项目中我觉得香港专业教育学院尝试了大约六个不同的方式,其中的这样这样,<一个href="http://stackoverflow.com/questions/1321248/how-do-i-save-serialize-a-custom-class-to-the-settings-file">this这样和<一href="http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/aa13ecaf-07a5-4833-bd0c-2cb73e5235dc">this这样但似乎没有任何工作...

目前的code符合,并且运行良好 - 在任何地方也不例外。 code自己保存的功能,但没有条目在设置xml文件创建(我有一些其他的设置,它适用于所有的人,但这个仅供参考)。当它加载, Properties.Settings.Default.LastSearches 总是空...有什么想法?

这是我的电流code:

的类:

  [Serializable接口]
公共类LastSearch:ISerializable的
{
    公开日期时间日期{获得;组; }
    公共字符串小时{获得;组; }
    公共字符串登录{获得;组; }
    公共字符串命令{获得;组; }
    公开名单&LT;字符串&GT; SelectedFilters {获得;组; }
    公开名单&LT;字符串&GT; SearchTerms {获得;组; }
    公开名单&LT;字符串&GT; HighlightedTerms {获得;组; }
    公开名单&LT;字符串&GT; ExcludedTerms {获得;组; }

    公共LastSearch(DateTime的日期,串小时,弦数,字符串命令,列表和LT;字符串&GT; selectedFilters,
        名单&LT;字符串&GT; sea​​rchTerms,列表和LT;字符串&GT; highlightedTerms,列表和LT;字符串&GT; excludedTerms)
    {
        日期= date.ToUniversalTime();
        小时=小时,
        登录=记录;
        命令=命令;
        SelectedFilters = selectedFilters;
        SearchTerms = searchTerms;
        HighlightedTerms = highlightedTerms;
        ExcludedTerms = excludedTerms;
    }

    保护LastSearch(SerializationInfo中的信息,的StreamingContext上下文)
    {
        日期= info.GetDateTime(日期);
        小时= info.GetString(一小时);
        登录= info.GetString(日志);
        命令= info.GetString(命令);
        SelectedFilters =(名单&LT;字符串&GT;)info.GetValue(SelectedFilters的typeof(名单&LT;字符串&GT;));
        SearchTerms =(名单&LT;字符串&GT;)info.GetValue(SearchTerms的typeof(名单&LT;字符串&GT;));
        HighlightedTerms =(名单&LT;字符串&GT;)info.GetValue(HighlightedTerms的typeof(名单&LT;字符串&GT;));
        ExcludedTerms =(名单&LT;字符串&GT;)info.GetValue(ExcludedTerms的typeof(名单&LT;字符串&GT;));
    }
    [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter =真)

    公共无效GetObjectData使用(SerializationInfo中的信息,的StreamingContext上下文)
    {
        info.AddValue(日期,日期);
        info.AddValue(一小时,一小时);
        info.AddValue(日志,日志);
        info.AddValue(命令,命令);
        info.AddValue(SelectedFilters,SelectedFilters);
        info.AddValue(SearchTerms,SearchTerms);
        info.AddValue(HighlightedTerms,HighlightedTerms);
        info.AddValue(ExcludedTerms,ExcludedTerms);
    }
}

[可序列化]
公共类LastSearchCollection:ISerializable的
{
    公开名单&LT; LastSearch&GT;搜索{获得;组; }

    公共LastSearchCollection()
    {
        搜索=新的名单,其中,LastSearch&GT;();
    }

    公共LastSearchCollection(SerializationInfo中的信息,的StreamingContext ctxt)
    {
        搜索=(名单&LT; LastSearch&GT;)info.GetValue(LastSearches的typeof(名单&LT; LastSearch&GT;));
    }
    [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter =真)

    公共无效GetObjectData使用(SerializationInfo中的信息,的StreamingContext上下文)
    {
        info.AddValue(搜索,搜索);
    }
}
 

写入设置:

 如果(RecentQueriesToolStripMenuItem.DropDownItems.Count大于0)
{
    //最后搜索设置
    如果(Properties.Settings.Default.LastSearches == NULL)
        Properties.Settings.Default.LastSearches =新LastSearchCollection();

    Properties.Settings.Default.LastSearches.Searches.Clear();
    的foreach(在RecentQueriesToolStripMenuItem.DropDownItems LastSearchMenuItem项)
    {
        Properties.Settings.Default.LastSearches.Searches.Add(item.SearchData);
    }
}

//保存所有设置
Properties.Settings.Default.Save();
 

加载从setttings

  //最后搜索
如果(Properties.Settings.Default.LastSearches!= NULL)
{
    INT I = 0;
    的foreach(在Properties.Settings.Default.LastSearches.Searches LastSearch搜索)
    {
        LastSearchMenuItem searchMenuItem =新LastSearchMenuItem(搜索);
        RecentQueriesToolStripMenuItem.DropDownItems.Add(searchMenuItem);
        RecentQueriesToolStripMenuItem.DropDownItems [I]。点击+ =新的EventHandler(RecentSearch_Click);
        我++;
    }
}
 

解决方案

在评论这个的链接对如何创建自定义的用户设置一个漂亮的简单的教程。

很高兴我能帮助。

I've been having a helluva time trying to add a custom collection of a custom class to the application settings of my winforms project I feel like Ive tried it about six different ways, including this way, this way, this way, and this way but nothing seems to work...

Currently the code complies, and runs fine - no exceptions anywhere. Code his the Save function but no entries are created in the settings xml file (I have a few other settings and it works for all of them but this one FYI). When it loads, Properties.Settings.Default.LastSearches is always null... Any thoughts?

Heres my current code:

The Classes:

[Serializable]
public class LastSearch : ISerializable
{
    public DateTime Date { get; set; }
    public string Hour { get; set; }
    public string Log { get; set; }
    public string Command { get; set; }
    public List<string> SelectedFilters { get; set; }
    public List<string> SearchTerms { get; set; }
    public List<string> HighlightedTerms { get; set; }
    public List<string> ExcludedTerms { get; set; }

    public LastSearch(DateTime date, string hour, string log, string command, List<string> selectedFilters,
        List<string> searchTerms, List<string> highlightedTerms, List<string> excludedTerms)
    {
        Date = date.ToUniversalTime();
        Hour = hour;
        Log = log;
        Command = command;
        SelectedFilters = selectedFilters;
        SearchTerms = searchTerms;
        HighlightedTerms = highlightedTerms;
        ExcludedTerms = excludedTerms;
    }

    protected LastSearch(SerializationInfo info, StreamingContext context)
    {
        Date = info.GetDateTime("Date");
        Hour = info.GetString("Hour");
        Log = info.GetString("Log");
        Command = info.GetString("Command");
        SelectedFilters = (List<string>)info.GetValue("SelectedFilters", typeof(List<string>));
        SearchTerms = (List<string>)info.GetValue("SearchTerms", typeof(List<string>));
        HighlightedTerms = (List<string>)info.GetValue("HighlightedTerms", typeof(List<string>));
        ExcludedTerms = (List<string>)info.GetValue("ExcludedTerms", typeof(List<string>));
    }
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Date", Date);
        info.AddValue("Hour", Hour);
        info.AddValue("Log", Log);
        info.AddValue("Command", Command);
        info.AddValue("SelectedFilters", SelectedFilters);
        info.AddValue("SearchTerms", SearchTerms);
        info.AddValue("HighlightedTerms", HighlightedTerms);
        info.AddValue("ExcludedTerms", ExcludedTerms);
    }
}

[Serializable]
public class LastSearchCollection : ISerializable
{
    public List<LastSearch> Searches { get; set; }

    public LastSearchCollection()
    {
        Searches = new List<LastSearch>();
    }

    public LastSearchCollection(SerializationInfo info, StreamingContext ctxt)
    {
        Searches = (List<LastSearch>)info.GetValue("LastSearches", typeof(List<LastSearch>));
    }
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Searches", Searches);
    }
}

Writing to Settings:

if (RecentQueriesToolStripMenuItem.DropDownItems.Count > 0)
{
    // Last Search Settings
    if (Properties.Settings.Default.LastSearches == null)
        Properties.Settings.Default.LastSearches = new LastSearchCollection();

    Properties.Settings.Default.LastSearches.Searches.Clear();
    foreach (LastSearchMenuItem item in RecentQueriesToolStripMenuItem.DropDownItems)
    {
        Properties.Settings.Default.LastSearches.Searches.Add(item.SearchData);
    }
}

// Save all settings
Properties.Settings.Default.Save();

Loading from setttings

// Last Searches
if (Properties.Settings.Default.LastSearches != null)
{
    int i = 0;
    foreach (LastSearch search in Properties.Settings.Default.LastSearches.Searches)
    {
        LastSearchMenuItem searchMenuItem = new LastSearchMenuItem(search);
        RecentQueriesToolStripMenuItem.DropDownItems.Add(searchMenuItem);
        RecentQueriesToolStripMenuItem.DropDownItems[i].Click += new EventHandler(RecentSearch_Click);
        i++;
    }
}

解决方案

as suggested in the comments this link on codeproject.com has a nice simple tutorial on how to create custom User Settings.

Glad I could help.

这篇关于添加自定义类Settings.Settings集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆