将列表保存到隔离存储设置

时间:2013-07-07 10:19:42

标签: c# .net wpf serialization windows-phone-8

我有这堂课:

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

这是包含项目LyricsItem的数组:

public List<LyricsItem> lyricsArray;

这是我向数组添加项目的方式:

    LyricsItem item = new LyricsItem();

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

    lyricsArrayTmp.Add(item);

我想将其添加到IsolatedStorageSettings

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

        appSettings.Save();

但是当我保存IsolatedStorageSettings时,我得到了这个例外:

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

1 个答案:

答案 0 :(得分:3)

您无法在ApplicationSettings中序列化私有类。将其声明为公开:

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