我正在开发一个音乐地铁风格的应用程序.我从用户音乐库获取所有音乐文件我想存储StorageFile对象,因为我不想一次又一次地检索.为此,我尝试序列化StorageFile对象并将其存储到XML中.在示例here和here中,我尝试生成XML...
我正在开发一个音乐地铁风格的应用程序.我从用户音乐库获取所有音乐文件我想存储StorageFile对象,因为我不想一次又一次地检索.为此,我尝试序列化StorageFile对象并将其存储到XML中.在示例here和here中,我尝试生成XML文件,但它在创建XML文件时引发异常
Type ‘Windows.Storage.StorageFile’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
到目前为止,我的代码如下,
namespace CloudMusic.AppSettings
{
[KnownType(typeof(CloudMusic.AppSettings.MusicFileDict))]
[DataContractAttribute]
public class MusicFileDict
{
[DataMember]
public object musicStorageFile { get; set; }
[DataMember]
public int id { get; set; }
}
}
从下面我正在生成XML
class GenerateMusicDict
{
private const string filename = "musiclist.xml";
static private List<MusicFileDict> _data = new List<MusicFileDict>();
static public List<MusicFileDict> Data
{
get { return _data; }
}
static async public Task Save<T>()
{
await Windows.System.Threading.ThreadPool.RunAsync((sender) => GenerateMusicDict.SaveAsync<T>().Wait(), Windows.System.Threading.WorkItemPriority.Normal);
}
internal static async Task SaveAsync<T>()
{
GenerateMusicDict.GenerateMusicFileDict();
try
{
StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
var sessionSerializer = new DataContractSerializer(typeof(List<MusicFileDict>), new Type[] { typeof(T) });
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
await sessionOutputStream.FlushAsync();
}
catch (Exception)
{
}
}
/// <summary>
/// Generate music file dictonary according to songs ID
/// </summary>
/// <param name="musicFileList"></param>
private static void GenerateMusicFileDict()
{
List<StorageFile> music_file_list = MusicFileDict.GetMusicList(); // At this stage i'll get all my music files,GetMusicList() function returns List<StorageFile>
for (int i = 0; i < music_file_list.Count; i++)
{
_data.Add(new MusicFileDict { id = i, musicStorageFile = music_file_list[i]});
}
}
}
并使用我的主类调用Save方法
await GenerateMusicDict.Save<MusicFileDict>();
在此行的SaveAsync()函数上
sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
它正在解雇一个例外
Type ‘Windows.Storage.StorageFile’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
so how can i serialize it? or is there any way that i can store it and re-use. Please help me
解决方法:
利用FutureAccessList http://msdn.microsoft.com/library/windows/apps/BR207457,它应该提供您正在寻找的功能.
您可以通过引用其路径来检索和访问文件.可以轻松地存储和序列化这些路径(例如,作为List).
如果您希望应用能够访问音乐库之外的用户内容,这还可以缓解您可能遇到的任何权限问题.
本文标题为:c# – 如何在metro应用程序中序列化Windows.Storage.StorageFile
基础教程推荐
- winform把Office转成PDF文件 2023-06-14
- C# List实现行转列的通用方案 2022-11-02
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- C# windows语音识别与朗读实例 2023-04-27
- unity实现动态排行榜 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- 一个读写csv文件的C#类 2022-11-06
- C#控制台实现飞行棋小游戏 2023-04-22
- C# 调用WebService的方法 2023-03-09