2011-08-09 10 views
8

He estado teniendo un gran problema tratando de agregar una colección personalizada de una clase personalizada a la configuración de la aplicación de mi proyecto de winforms Siento que lo he intentado de seis maneras diferentes , incluyendo this way, this way, this way y this way pero nada parece funcionar ...Agregue una colección de una clase personalizada a Settings.Settings

Actualmente el código cumple, y funciona muy bien - no hay excepciones en cualquier lugar. Codifique su función Guardar, pero no se crean entradas en el archivo xml de configuración (tengo algunas otras configuraciones y funciona para todas ellas excepto esta para su información). Cuando se carga, Properties.Settings.Default.LastSearches siempre es nulo ... ¿Alguna idea?

Heres mi código actual:

Las Clases:

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

Escribiendo a Ajustes:

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

Carga de 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++; 
    } 
} 
+0

¿Puedo preguntar por qué está tratando de almacenar esto en la configuración de usuario? ¿Por qué no simplemente serializar su clase en el disco? – Jethro

+0

@Jethro Eso es exactamente lo que realmente hace la configuración del usuario integrada. Ya tengo todas mis otras configuraciones allí y tenía sentido tratar de mantener todo junto. – Hershizer33

+0

¿Has visto este artículo sobre cómo mantener la clase personalizada? http://www.codeproject.com/KB/cs/WinAppUserSettings.aspx – Jethro

Respuesta

7

según lo sugerido en los comentarios este link en codeproject.com tiene un tutorial simple y agradable sobre cómo crear configuraciones de usuario personalizadas.

Me alegro de poder ayudar.

Cuestiones relacionadas