简体   繁体   中英

Save List to IsolatedStorageSettings

I have this class :

class LyricsItem
{
    public LyricsItem()
    {

    }

    public LyricsItem(LyricsItem item)
    {
        this.searchUrl = item.searchUrl;
        this.croppingRegex = item.croppingRegex;
    }

    private string _searchUrl;
    private string _croppingRegex;

    public string searchUrl
    {
        get { return _searchUrl; }
        set { _searchUrl = value; }
    }

    public string croppingRegex
    {
        get { return _croppingRegex; }
        set { _croppingRegex = value; }
    }
}

And this is Array with items LyricsItem :

public List<LyricsItem> lyricsArray;

This is how i add items to the array :

    LyricsItem item = new LyricsItem();

    item.croppingRegex = croppingRegex;
    item.searchUrl = searchurl;

    lyricsArrayTmp.Add(item);

And i want to add it to IsolatedStorageSettings :

        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
        if (appSettings.Contains("lyricsData"))
        {
            appSettings["lyricsData"] = lyricsArray;
        }
        else
        {
            appSettings.Add("lyricsData", lyricsArray);
        }

        appSettings.Save();

But When i get to save the IsolatedStorageSettings i get this exception:

The collection data contract type 'System.Collections.Generic.List`1[[**********, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be deserialized because it does not have a public parameterless constructor. Adding a public parameterless constructor will fix this error. Alternatively, you can make it internal, and use the InternalsVisibleToAttribute attribute on your assembly in order to enable serialization of internal members - see documentation for more details

You can't serialize a private class in the ApplicationSettings. Declare it as public instead:

public class LyricsItem
{
    public LyricsItem()
    {

    }

    public LyricsItem(LyricsItem item)
    {
        this.searchUrl = item.searchUrl;
        this.croppingRegex = item.croppingRegex;
    }

    private string _searchUrl;
    private string _croppingRegex;

    public string searchUrl
    {
        get { return _searchUrl; }
        set { _searchUrl = value; }
    }

    public string croppingRegex
    {
        get { return _croppingRegex; }
        set { _croppingRegex = value; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM