0

我有这门课:

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
4

1 回答 1

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; }
    }
}
于 2013-07-07T10:39:01.477 回答