“由于对象的当前状态,操作无效。”尝试创建新List<时出错AudioTrack>来自WP8中的JsonFile

时间:2014-09-23 03:43:38

标签: c# json windows-phone-8

我正在尝试创建一个音乐应用程序,我在解决方案中有2个项目:一个主项目和一个AudioPlaybackAgent(APA)项目。当我从主项目(包含在歌曲列表中)中选择一首歌曲时,我将整个列表复制到ISolate存储中的Json文件中,并从我的APA项目中读取它。

问题是,当我尝试从json文件中的项目创建新的AudioTrack时出现错误

创建文件的代码(来自我的主项目)

public static void CreateOnlineList(string id, ObservableCollection<BasicItemModel> list)
    {
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
        {
            string fileName = "OnlineList.json";

            JObject obj = new JObject();                
            JArray arr = new JArray();
            for (int dem = 0; dem < list.Count(); dem++)
            {
                if (list[dem].Id == id) 
                    obj.Add("index", dem);
                JObject item = new JObject();                    
                item.Add("name", list[dem].Name);
                item.Add("artist", list[dem].Artist);
                item.Add("url", list[dem].Url);
                arr.Add(item);
            }
            obj.Add("list", arr);
            string jsonString = JsonConvert.SerializeObject(obj);
            using (IsolatedStorageFileStream stream = file.OpenFile(fileName,FileMode.OpenOrCreate,FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine(jsonString);                        
                }
            }
        }
    }

读取此文件的代码(来自APA项目)

public static void ReadOnlineList()
    {
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
        {
            string fileName = "OnlineList.json";
            string jsonString = "";
            using (IsolatedStorageFileStream stream = file.OpenFile(fileName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader writer = new StreamReader(stream))
                {
                    jsonString = writer.ReadLine();
                }
            }
            JObject obj = new JObject();
            obj = JObject.Parse(jsonString);
            AudioPlayer.currentTrackNumber = int.Parse(obj["index"].ToString());
            List<JToken> arr = obj["list"].ToList();
            if (AudioPlayer.playList == null) AudioPlayer.playList = new List<AudioTrack>();
            else AudioPlayer.playList.Clear();
            foreach (var item in arr)
            {
                AudioTrack track = new AudioTrack()
                {
                    Source = new Uri(item["url"].ToString(), UriKind.Absolute),
                    Title = item["name"].ToString(),
                    Artist = item["artist"].ToString(),
                    Album = null
                };     
                //// I get error of this line when trying to create an track
                AudioPlayer.playList.Add(track);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

AudioTrack对象有一些奇怪的行为,有两种方法可以从AudioTrack插入/编辑数据。

在这种情况下,您正在访问其属性,并且您将生成错误。

你可以做以下两件事之一:

选项1 - 在构造函数中插入数据

AudioTrack track = new AudioTrack(new Uri(item["url"].ToString(), UriKind.Absolute), item["artist"].ToString(), null, null);

选项2 - 使用BeginEdit()和EndEdit()方法

AudioTrack track = new AudioTrack();
track.BeginEdit();
track.Source = new Uri(item["url"].ToString(), UriKind.Absolute);
track.Title = item["name"].ToString();
track.Artist = item["artist"].ToString();
track.EndEdit();

我希望这有助于回答你的问题。