Mam hellva czas próbując dodać niestandardową kolekcję niestandardowej klasy do ustawień aplikacji mojego projektu WinFormy Czuję, że Ive próbował go na sześć różnych sposobów , w tym this way, this way, this way i this way ale nic nie wydaje się działać ...Dodaj kolekcję niestandardowej klasy do Settings.Settings
Obecnie kod zgodny i działa dobrze - nie ma wyjątków wszędzie. Zakoduj jego funkcję Save, ale w pliku ustawień xml nie zostaną utworzone żadne wpisy (mam kilka innych ustawień i działa dla nich wszystkich, ale ten jeden dla ciebie). Kiedy się ładuje, Properties.Settings.Default.LastSearches
jest zawsze pusta ... Jakieś myśli?
Herezje mój bieżący kod:
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);
}
}
Pisząc do Ustawienia:
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();
Ładowanie z 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++;
}
}
Czy mogę zapytać, dlaczego próbujesz to zapisać w ustawieniach użytkownika? Dlaczego nie po prostu serializować klasy na dysk? – Jethro
@Jethro Dokładnie tak działa wbudowane ustawienie użytkownika. Mam już wszystkie inne moje ustawienia i sensowne jest, aby spróbować zachować wszystko razem. – Hershizer33
Czy widziałeś ten artykuł o utrzymywaniu niestandardowej klasy? http://www.codeproject.com/KB/cs/WinAppUserSettings.aspx – Jethro