0

我已经查看了我能找到的关于序列化的所有内容,但不确定为什么这不起作用。我序列化了我的 myMediaInterface 对象列表,它是视频和歌曲类的基类。我把歌曲类放在下面。所有为 null 的属性都被序列化到 xml 文件中,但所有具有值的属性都没有。在调用序列化代码时,列表中有 882 首歌曲,并且 xml 文件中出现了 882 首序列化歌曲,但没有任何非 null 或 0 属性。

[XmlInclude(typeof(Song))]
[XmlInclude(typeof(Video))]

public abstract class myMediaInterface
{
    public string type { get; set; }
    public string path { get; set; }
    public String artist { get; set; }
    public String title { get; set; }
    public String album { get; set; }
    public uint trackNumber { get; set; }
    public String genre1 { get; set; }
    public TimeSpan duration { get; set; }
    public uint rating { get; set; }
    public uint bitrate { get; set; }
    public uint year { get; set; }
    public List<String> genre { get; set; }
    public int indexWithinParentCollection { get; set; }
    public string displayName { get; set; }
}

public class Song : myMediaInterface
{
    public String type
    {
        get
        {
            return "Song";
        }
        set
        {
            value = "Song";
        }
    }
    public String path { get; set; }
    public String artist { get; set; }
    public String title { get; set; }
    public String album { get; set; }
    public uint trackNumber { get; set; }
    public String genre1 { get; set; }
    public TimeSpan duration { get; set; }
    public uint rating { get; set; }
    public uint bitrate { get; set; }
    public uint year { get; set; }
    public List<String> genre { get; set; }
    public int indexWithinParentCollection { get; set; }
    public String displayName { get; set; }
}

序列化代码:

    static async private Task saveState()
    {

        try
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(List<myMediaInterface>), new Type[]{typeof(Song)});
            StorageFile musicFile = await KnownFolders.MusicLibrary.CreateFileAsync("PlaylistXFastMusicLoading", CreationCollisionOption.ReplaceExisting);
            IRandomAccessStream musicFileRandomAccess = await musicFile.OpenAsync(FileAccessMode.ReadWrite);
            IOutputStream outputStream = musicFileRandomAccess.GetOutputStreamAt(0);
            xmlSer.Serialize(outputStream.AsStreamForWrite(), _musicList);
            outputStream.Dispose();
        }
        catch (Exception exception)
        {
            System.Diagnostics.Debug.WriteLine(exception);
        }
    }

async private void loadState()
    {
        try
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(List<myMediaInterface>), new Type[] { typeof(Song) });
            StorageFile sampleFile = await KnownFolders.MusicLibrary.CreateFileAsync("PlaylistXFastMusicLoading", CreationCollisionOption.OpenIfExists);
            IInputStream fileStream = await sampleFile.OpenReadAsync();
            _musicList = (List<myMediaInterface>)xmlSer.Deserialize(fileStream.AsStreamForRead());
            fileStream.Dispose();
        }
        catch (Exception exception)
        {
            System.Diagnostics.Debug.WriteLine(exception);
        }
   }

和列表类初始化

    public static List<myMediaInterface> musicList = new List<myMediaInterface>();
    public static List<myMediaInterface> _musicList
    {
        get { return musicList; }
        set { value = musicList; }
    }
4

1 回答 1

0

您正在重新实现并隐藏从 myMediaInterface 继承的所有成员。这意味着您在基类和继承类中重复了相同的字段。我想你想要这个:

[XmlInclude(typeof(Song))]
public abstract class myMediaInterface
{
    public abstract string type { get; set; }
    public string path { get; set; }
    public String artist { get; set; }
    public String title { get; set; }
    public String album { get; set; }
    public uint trackNumber { get; set; }
    public String genre1 { get; set; }
    public TimeSpan duration { get; set; }
    public uint rating { get; set; }
    public uint bitrate { get; set; }
    public uint year { get; set; }
    public List<String> genre { get; set; }
    public int indexWithinParentCollection { get; set; }
    public string displayName { get; set; }
}

public class Song : myMediaInterface
{
    public override String type
    {
        get
        {
            return "Song";
        }
        set
        {
            value = "Song";
        }
    }
}

话虽如此,我认为您不需要属性“类型”来识别对象的类型,您可以使用 object.GetType() 获取对象的类型。

于 2013-08-06T03:16:37.880 回答