WP8使用硬件后退按钮后无法更新XML文件中的列表框项

时间:2015-04-27 09:52:49

标签: windows-phone-8

大家好我有一个列表框,我在XML文件中保存列表框选中的项目工作正常,但问题是,当我将关闭我的应用程序并重新打开并向列表框添加更多值时,我的前一个值被删除表单xml文件和列表框也是我如何在xml文件中保存我当前的添加值和以前的值我正在使用以下代码:

 <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"  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>

我正在使用以下后端代码:

 private void list_location_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {

        if (MessageBox.Show(MsgConst.ADD_LOCATION, "Location", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        {

                try
                {
                    //list_locationAdd.Items.Add(list_location.SelectedItem); 

                    Prediction pItem = list_location.SelectedItem as Prediction;
                    App.professionalId = pItem.description;

                    list_locationAdd.ItemsSource = App.professionalId;
                    list_locationAdd.Visibility = Visibility.Visible;
                    list_location.Visibility = Visibility.Collapsed;
                    XmlWriterSettings x_W_Settings = new XmlWriterSettings();
                    x_W_Settings.Indent = true;
                    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream stream = ISF.OpenFile("La.xml", FileMode.Create))
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Prediction>));
                            using (XmlWriter xmlWriter = XmlWriter.Create(stream, x_W_Settings))
                            {
                                if (list_locationAdd != null)
                                {
                                    data.Add(new Prediction() { description = App.professionalId });
                                    list_locationAdd.ItemsSource = data;
                                    serializer.Serialize(xmlWriter, data);

                                }


                            }

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        try
        {

                using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream str = ISF.OpenFile("La.xml", 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;
                        }

                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

在xml中添加值时,第二次打开应用程序时,可能会刷新xml文件中已存在的数据。

检查将所选值添加到xml文件中的后端代码。你可能会发现一些问题。

可能的解决方案: -

每次应用打开时都不要重写整个xml文件,只需将值附加到xml文件的现有内容中。

希望有帮助..! 如需进一步的帮助,请在此处提供您的Back-End代码。