从序列化列表中删除项目不起作用

时间:2015-08-05 09:15:10

标签: c# list serialization windows-phone-8.1

我在从序列化列表中删除项目时遇到问题 在写入文件时,VS给了我这条消息:

  

发生了'System.Runtime.Serialization.SerializationException'类型的第一次机会异常

我正在尝试执行的操作是从列表中选择一个项目,在另一个框架中详细显示它(这是有效的),然后让用户删除此项目。 这是我用来删除项目的代码:

private async void DeleteRecipe(object sender, RoutedEventArgs e)
{
    MessageDialog dialog = new MessageDialog("Do you want to delete this recipe?");
    dialog.Commands.Add(new UICommand(("Yes"), new UICommandInvokedHandler(this.CommandInvokedHandler)));
    dialog.Commands.Add(new UICommand(("No"), new UICommandInvokedHandler(this.CommandInvokedHandler)));
    await dialog.ShowAsync();
}

private async void CommandInvokedHandler(IUICommand command)
{
    if (command.Label.Equals("Yes"))
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Recipe>));
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("recipes.json");
        IRandomAccessStream randomAccessStream = await file.OpenAsync(FileAccessMode.Read);
        using (Stream stream = randomAccessStream.AsStreamForRead())
        {
            myRecipes = serializer.ReadObject(stream) as List<Recipe>;
        }
        randomAccessStream.Dispose();

        DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(List<Recipe>));
        StorageFile myList = await ApplicationData.Current.LocalFolder.CreateFileAsync("recipes.json",
            CreationCollisionOption.OpenIfExists);

        IRandomAccessStream randomAccessStream1 = await myList.OpenAsync(FileAccessMode.ReadWrite);

        using (Stream stream = randomAccessStream1.AsStreamForWrite())
        {
            myRecipes.Remove(recipe);
            serializer1.WriteObject(stream, myRecipes);
            await stream.FlushAsync();
        }
        randomAccessStream.Dispose();
        MessageDialog msg = new MessageDialog("Recipe deleted.");
        await msg.ShowAsync();
    }
}

myRecipes是序列化列表,recipe是表示我已详细打开的列表元素的对象。

我真的不知道我哪里出错了,在我添加这些命令之前,应用程序在序列化和向列表中添加项目时工作正常。

编辑:同样,如果我这样放myRecipes.Remove(recipe)

using (Stream stream = randomAccessStream.AsStreamForRead())
            {
                myRecipes = serializer.ReadObject(stream) as List<Recipe>;
            }
            randomAccessStream.Dispose();

            myRecipes.Remove(recipe);

            DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(List<Recipe>));
            StorageFile myList = await ApplicationData.Current.LocalFolder.CreateFileAsync("recipes.json",
                CreationCollisionOption.OpenIfExists);

而不是像上面那样:

using (Stream stream = randomAccessStream1.AsStreamForWrite())
            {                   
                serializer1.WriteObject(stream, myRecipes);
                await stream.FlushAsync();
            }

不抛出异常但它不会从列表中删除该项。

编辑更多代码:

这是我在列表中添加新项目的方式:

 public sealed partial class AddNewRecipe : Page
{
    [DataMember]
    List<Recipe> myRecipes;

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(List<Recipe>));
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("recipes.json");
        IRandomAccessStream randomAccessStream1 = await file.OpenAsync(FileAccessMode.Read);
        using (Stream stream = randomAccessStream1.AsStreamForRead())
        {
            myRecipes = serializer1.ReadObject(stream) as List<Recipe>;
        }

        randomAccessStream1.Dispose();

    }
private async void AddRecipe(object sender, RoutedEventArgs e)
    {


            String name = RecipeName.Text;
            String description = RecipeDescription.Text;               
            myRecipes.Add(new Recipe(name, description,ingredients));



            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Recipe>));
            StorageFile myList = await ApplicationData.Current.LocalFolder.CreateFileAsync("recipes.json", 
                CreationCollisionOption.OpenIfExists);

            IRandomAccessStream randomAccessStream = await myList.OpenAsync(FileAccessMode.ReadWrite);

            using (Stream stream = randomAccessStream.AsStreamForWrite())
            {
                serializer.WriteObject(stream, myRecipes);
                await stream.FlushAsync();
            }
            randomAccessStream.Dispose();

            Frame frame = Window.Current.Content as Frame; //get current frame  

            if (frame.CanGoBack)
            {
                frame.GoBack();                  
            }
            Frame.BackStack.RemoveAt(Frame.BackStack.Count - 1);
}

我用另一种方法管理成分,这里不是那么重要。 但是,我也注意到了其他的东西:在我删除一个项目后它给了我

  

SYSTEM.SERVICEMODEL.WEB.NI.DLL中发生了'System.Runtime.Serialization.SerializationException'类型的第一次机会异常

如果我尝试关闭应用并再次打开它,它会在尝试加载列表时给出同样的错误,所以我怀疑序列化中存在问题......?

0 个答案:

没有答案