0

I'm just starting to work with windows 8 development (using c#), and am working on porting one of my applications to a windows store app. Essentially my application takes a user input string, calculates an appropriate output, and responds to the user. So I'm using a list view to track the "conversation." I display the user's input as a list view element and then display the output as the next element (using text blocks with formatting like color...).

What I would like to do is make this "conversation" persist once the app is terminated but am not sure the best way to do this. My first thought was to serialize the list view object on suspension then deserialize it on load, but the listview isn't serializable...So my next thought was to write out each input and output string to a file and try to rebuild the listview from these strings when I load the application, but I am having some issues with this as well.

So I'm curious as to what the best way to go about this is. I would like the application to start back up with the previous conversation already displayed with the same formatting and what not. Does anyone have any ideas?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Nelz11
  • 3,066
  • 1
  • 14
  • 20

1 Answers1

2

I wouldn't persist the ListView anyway, you only need to persist the data in it. Are you using a MVVM type model, where your conversation is perhaps captured as an ObservableCollection and then bound to the ListView? If not, you should :)

Then you'd just need to serialize the ObservableCollection (see one option for that here). Where you persist it kind of depends on you:

  • LocalFolder would use a file based approach and be available on the local device
  • RoamingFolder would also use a file based approach but sync using the cloud across multiple devices that the user owns (and has the app installed)
  • Cloud storage (like Windows Azure or Windows Azure Mobile Services) would provide essentially boundless storage for you, but requires managing a cloud account and paying for it (though free tiers may be sufficient)
  • An in-memory database like SQLite is yet another option and would give you relational semantics should that be interesting to you.

To get started, I'd say use LocalFolder and persist your collection to a file, then when you rehydrate it, simple data binding should automatically handle the display. At some point, you may need to make a decision about how much to store. You want your application to be responsive for the user ('fast and fluid'), so to that end you may need to bring in data as it's requested versus all at once (perhaps a version 2 feature!)

Community
  • 1
  • 1
Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
  • Thanks for the suggestion. I'm new enough to this (two days in!) that I'm not real sure how the ObservableCollection works, nor how the data bindings work in the view. Do you have any suggestions on places to start? Also does this allow me to store the formatting of the text blocks? Or would I have to re-create this as I rehydrate? – Nelz11 Dec 21 '12 at 03:53
  • Yeah, take a look at how the built-in projects in Visual Studio work. The idea is that your formatting is all part of the design (XAML) and by wiring up binding references to objects in a collection it all just magically works. Microsoft's [Generation App](http://aka.ms/8in30) program is a great way to get introduced to all of the features and patterns. The [Contoso Cookbook](http://fb.me/2rpGarDPb) is a great sample app to consult as well, and look at samples on the Dev Center (dev.windows.com). Search for MVVM and Windows 8 for more. – Jim O'Neil Dec 21 '12 at 04:02
  • Interesting. I was handling my formatting in the code-behind because I needed alternating formatting, and from a couple hours of google searching, couldn't find a good way to get my list view members to format with alternating styles from within the xaml. – Nelz11 Dec 21 '12 at 04:05
  • 1
    ItemTemplateSelector will be key here, [here's a sample](http://www.comyoucom.com/implementing-a-custom-datatemplateselector-in-winrt/) that should convey the general approach – Jim O'Neil Dec 21 '12 at 04:17