I have a ListView in a UWP application with many ListViews as its ListViewItems. The ListViews inside the main ListView are lists of custom ListViewItems (with a grid with elements as their content).
The user can always add or remove either the custom ListViewItems in the ListViews in the main ListView or the entire ListViews in the main ListView.
I wanted to save all the changes made by user in application's settings or in a file (to save the created ListViews and custom ListViewItems and load them on start)
Is there a way to do that?
What I've tried:
JsonConvert.serializeObject()
) resulted in a Stack Overflow exception Please provide the code showing how to save and load the ListView
The application structure looks like this:
Main ListView (that should be saved) > Main ListView items (ListViews) (can be added/removed by user) > Custom ListViewItems (not only strings) (can be added/removed by user)
For your requirement, you need design nesting listview with datasource and store the datasource to the file. And as Klaus Gütter said, ListViewItems does not support to serialize.
For example
Xaml code
<Grid>
<ListView ItemsSource="{x:Bind Items}">
<ListView.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="SaveList" Click="Button_Click"/>
</Grid>
Code behind
public sealed partial class MainPage : Page
{
public ObservableCollection<ObservableCollection<ListContent>> Items { set; get; }
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Object value = localSettings.Values["exampleSetting"];
if (value == null)
{
var list1 = new ObservableCollection<ListContent>() { new ListContent { Content = "1" }, new ListContent { Content = "2" }, new ListContent { Content = "3" } };
var list2 = new ObservableCollection<ListContent>() { new ListContent { Content = "A" }, new ListContent { Content = "B" }, new ListContent { Content = "C" } };
var list3 = new ObservableCollection<ListContent>() { new ListContent { Content = "一" }, new ListContent { Content = "二" }, new ListContent { Content = "三" } };
Items = new ObservableCollection<ObservableCollection<ListContent>>() { list1, list2, list3 };
}
else
{
Items = JsonConvert.DeserializeObject<ObservableCollection<ObservableCollection<ListContent>>>(value.ToString());
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string json = JsonConvert.SerializeObject(Items);
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
// Create a simple setting.
localSettings.Values["exampleSetting"] = json;
// Read data from a simple setting.
}
}
public class ListContent
{
public string Content { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.