我正在开发一个音乐地铁风格的应用程序.我从用户音乐库获取所有音乐文件我想存储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


基础教程推荐
- 实例详解C#实现http不同方法的请求 2022-12-26
- C# 解析XML和反序列化的示例 2023-04-14
- Unity shader实现高斯模糊效果 2023-01-16
- C#中 Json 序列化去掉null值的方法 2022-11-18
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#调用摄像头实现拍照功能的示例代码 2023-03-09