大家好我有一个列表框,我在XML文件中保存列表框选中的项目工作正常,但问题是,当我将关闭我的应用程序并重新打开并向列表框添加更多值时,我的前一个值被删除表单xml文件和列表框也是我如何在xml文件中保存我当前的添加值和以前的值我正在使用以下代码:
和iam使用以下代码
<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>
我的后端代码如下:
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)
{
}
}
答案 0 :(得分:0)
按以下方式更改代码:
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);
}
}
更新当前代码:
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
if (isoStore.FileExists(filename))
{
try
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, isoStore))
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Prediction>));
ObservableCollection<Prediction> data = (ObservableCollection<Prediction>)serializer.Deserialize(isoStream);
this.list_locationAdd.ItemsSource = data;
}
}
catch(Exception ex)
{ }
}
else
{
try
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore))
{
XmlSerializer Serialize = new XmlSerializer(typeof(ObservableCollection<Prediction>));
using (XmlWriter writer = XmlWriter.Create(isoStream, x_W_Settings))
{
data.Add(new Prediction() { description = App.professionalId });
list_locationAdd.ItemsSource = data;
Serialize.Serialize(writer, data);
}
}
}
catch (Exception ex)
{ }
}
希望这对你有所帮助。