为什么我的属性不被序列化,winrt c#xaml

时间:2013-08-06 02:19:25

标签: c# winrt-xaml

我已经查看了有关序列化的所有内容,但不确定为什么这不起作用。我序列化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; }
    }

1 个答案:

答案 0 :(得分: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";
        }
    }
}

说,我认为你不需要有一个属性“type”来识别你的对象的类型,你可以用object.GetType()来获取你的对象的类型。