我正在开发一个音乐地铁风格的应用程序.我从用户音乐库获取所有音乐文件我想存储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# 8中如何使用默认接口方法详解 2023-03-29
- Oracle中for循环的使用方法 2023-07-04
- c# 模拟线性回归的示例 2023-03-14
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- .NET CORE DI 依赖注入 2023-09-27
- Unity Shader实现模糊效果 2023-04-27
- 如何使用C# 捕获进程输出 2023-03-10
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- Unity3D实现渐变颜色效果 2023-01-16
 
						 
						 
						 
						 
						 
				 
				 
				 
				