Ajouter une collection d'une classe personnalisée des Paramètres.Paramètres

J'ai eu un sacré temps à essayer d'ajouter une collection personnalisée d'une classe personnalisée pour les paramètres de l'application de mon projet winforms je me sens comme j'ai essayé de ça à propos de six façons différentes, y compris de cette façon, de cette façon, de cette façon, et de cette façon mais rien ne semble fonctionner...

Actuellement, le code est conforme et fonctionne très bien - pas d'exceptions partout. Code son la fonction d'enregistrement, mais pas les entrées sont créées dans le fichier xml de paramètres (j'ai quelques autres paramètres, et il fonctionne pour eux tous, mais ce un FYI). Quand il charge, Properties.Settings.Default.LastSearches est toujours nul... des idées?

Voilà mon code actuel:

Les 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);
}
}

Écriture de Paramètres:

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();

Chargement de paramètres

//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++;
}
}
puis-je vous demander pourquoi vous essayez de stocker cela dans les paramètres de l'Utilisateur? Pourquoi ne pas simplement sérialiser votre classe sur le disque?
C'est exactement ce que le construit en paramètre de l'utilisateur n'est vraiment. J'ai déjà toutes mes autres paramètres, il fait sens pour essayer de le garder ensemble.
Avez-vous vu cet article sur comment persister classe personnalisée. codeproject.com/KB/cs/WinAppUserSettings.aspx
Merci! Cependant je n'ai toujours vouloir essayer de faire ce travail avec les construit dans les paramètres
Eh bien j'ai fini à l'aide de votre lien, donc si vous mettez que comme une réponse Mal à l'accepter

OriginalL'auteur Hershizer33 | 2011-08-09