简体   繁体   中英

Windows 8 C# - how to keep ListBox items?

let's say I have a ListBox written in XAML:

<ListBox x:Name="myListBox" />

and add items in C#:

myListBox.Items.Add("a");
myListBox.Items.Add("b");
myListBox.Items.Add("c");

When I close the app, the items I have added in myListBox are all gone and when I open the app again, the myListBox starts from blank. I wanted to keep the items, right now my method is to store it in 1 string and save it to RoamingSettings. But it is inefficient, my code are mess now. Is there a better idea?

There are a variety of ways to save your application's state, as detailed in this article: http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-7

Are you overriding the SaveState method? That method is provided with a Dictionary<String,Object> you can use to store data more appropriately than manually serializing to RoamingSettings.

That said, consider adopting MVVM design, that way you only need to save your view/page's ViewModel data rather than control property state.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM