2011-08-09 12 views
8

मेरे पास हेल्लूवा समय है जो मेरे विनफॉर्म प्रोजेक्ट की एप्लिकेशन सेटिंग्स में कस्टम क्लास का कस्टम संग्रह जोड़ने का प्रयास कर रहा है, मुझे लगता है कि मैंने इसे छह अलग-अलग तरीकों से आजमाया है , this way, this way, this way, और this way लेकिन कुछ भी सहितसेटिंग्स में कस्टम क्लास का संग्रह जोड़ें। सेटिंग्स

वर्तमान में कोड का अनुपालन काम करने के लिए लगता है ..., और ठीक चलाता है - कहीं भी कोई अपवाद नहीं। कोड को सहेजें फंक्शन करें लेकिन सेटिंग्स xml फ़ाइल में कोई प्रविष्टियां नहीं बनाई गई हैं (मेरे पास कुछ अन्य सेटिंग्स हैं और यह उन सभी के लिए काम करती है लेकिन यह एक एफवाईआई है)। जब यह लोड होता है, Properties.Settings.Default.LastSearches हमेशा शून्य होता है ... कोई विचार?

यहाँ मेरे वर्तमान कोड:

क्लास:

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

सेटिंग पर लेखन:

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

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

क्या मैं पूछ सकता हूं कि आप इसे उपयोगकर्ता सेटिंग्स में क्यों स्टोर करने का प्रयास कर रहे हैं? क्यों न केवल अपनी कक्षा को डिस्क पर क्रमबद्ध करें? – Jethro

+0

@ जेथ्रो यह वास्तव में बनाया गया है कि उपयोगकर्ता सेटिंग में वास्तव में क्या किया गया है। मेरे पास पहले से ही मेरी सभी अन्य सेटिंग्स हैं और इसे सभी को एक साथ रखने और इसे रखने के लिए समझ में आया। – Hershizer33

+0

क्या आपने इस लेख को कस्टम क्लास को बनाए रखने के तरीके पर देखा है। http://www.codeproject.com/KB/cs/WinAppUserSettings.aspx – Jethro

उत्तर

7

टिप्पणियों में सुझाव दिया गया है कि कोडप्रोजेक्ट.com पर link पर कस्टम उपयोगकर्ता सेटिंग्स बनाने के तरीके पर एक अच्छा सरल ट्यूटोरियल है।

खुशी है कि मैं मदद कर सकता हूं।

संबंधित मुद्दे