locked
Windows Phone 8 Can't Update listbox item in XML file after using hardware back button?

    Question

  • Hi All i have a list box and i am saving list box selected item in XML file thats working fine but the problem is that when i will close my app and reopen and add more value to list box my previous value is removed form the xml file and listbox also how i can save my current added value and previous value in xml file i am using following code :

    and iam using following code

    <Grid x:Name="ContentPanel" Grid.Row="2" Margin="15,10,15,0">
                <ListBox Name="list_location" Tap="list_location_Tap"  Foreground="Black">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock x:Name="item_name" Text="{Binding description, Mode=OneWay}" Padding="5,15,5,15" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
    
                </ListBox>
    
                <ListBox Name="list_locationAdd" Background="Red"  Foreground="Black" Visibility="Collapsed">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock x:Name="item_name" Text="{Binding description, Mode=OneWay}" Padding="5,15,5,15" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    
            </Grid>
    

    and my back end code is follow:

     XmlWriterSettings x_W_Settings = new XmlWriterSettings();
                            x_W_Settings.Indent = true;
                            using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                using (IsolatedStorageFileStream stream = ISF.OpenFile(filename, FileMode.Create))
                                {
                                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Prediction>));
                                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, x_W_Settings))
                                    {
    
                                            data.Add(new Prediction() { description = App.professionalId });
                                            list_locationAdd.ItemsSource = data;
                                            serializer.Serialize(xmlWriter, data);
    
    
    
                                    }
                                }
                            }
    
     protected override void OnNavigatedTo(NavigationEventArgs e)
        {
    
            try
            {
                if (list_locationAdd != null)
                {
    
    
                    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream str = ISF.OpenFile(filename, FileMode.Open))
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Prediction>));
                            ObservableCollection<Prediction> data = (ObservableCollection<Prediction>)serializer.Deserialize(str);
                            if (list_locationAdd != null)
                            {
                                this.list_locationAdd.ItemsSource = data;
                                list_locationAdd.Visibility = Visibility.Visible;
                            }
    
                        }
                    }
                }
            }
            catch (Exception ex)
            {
    
            }
    
        }
    

    Wednesday, April 29, 2015 10:09 AM

All replies

  • Can you provide a working sample?  Upload to Onedrive and share it with us.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 29, 2015 5:03 PM
    Moderator