Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Preparing for persistence

From: Windows Phone SDK Essential Training

Video: Preparing for persistence

To begin moving the Sonnet's application into a multipage application using pivot with persistence, we've started out and created a new solution called Sonnets Plus flat file. And we've copied over some of the things from the Sonnets app that we built previously. We've got the original Shakespeare Sonnets in CML form. We've added two images. One is the star that shows up down in the application bar so that we can choose a favorite. And we've also added a yellow version of that for use on the page which works in both the light theme and the dark theme. So we couldn't just use this image here, the appbar.favs.rest.png, because in the light theme, it wouldn't show up.

Preparing for persistence

To begin moving the Sonnet's application into a multipage application using pivot with persistence, we've started out and created a new solution called Sonnets Plus flat file. And we've copied over some of the things from the Sonnets app that we built previously. We've got the original Shakespeare Sonnets in CML form. We've added two images. One is the star that shows up down in the application bar so that we can choose a favorite. And we've also added a yellow version of that for use on the page which works in both the light theme and the dark theme. So we couldn't just use this image here, the appbar.favs.rest.png, because in the light theme, it wouldn't show up.

When you use an icon from the Icons directory in the Application bar, the operating system automatically switches it when you change light and dark themes. But it doesn't, of course, change it on your application content area, because you want to have full control over that. So we took that, filled it in with yellow, and named it Favorite. We've also copied over the About Page so that we don't have to do that all over again. And now, what we're going to do is look at, we've also added a couple of references. Because the way we're going to store our list of sonnets is we're going to store it in an isolated storage file as a flat XML file.

And in order to do that, we brought in system.xml, system.xml.link and system.xml.serialization using isolated storage in this example. Because it works with both Windows Phone 7 and Windows Phone 8. If you're targeting Windows Phone 8 only, you should investigate the windows.storage name space because that's the one that's compatible with Windows 8. And will be the one that'll be used exclusively on Windows phone moving forward in the next generations of Windows phone. We've also, because we're going to be building two versions of this app, one that uses a flat file and one that uses the on-board SQL CE database.

We've made a couple of minor changes to the WMAP manifest, changing the title of the app to Sonnets plus FF. And changing the title of the large background icon when you pin to the homepage to Sonnets plus FF. Alright, so the first thing we're going to do is we're going to add a class which is going to hold our data that we're going to persist. So, we're going to add a new class and we're going to call it Sonnets. And in Sonnets, we're just going to create a collection of properties.

Because if we're going to be serializing and de-serializing this information back and forth with XML. So we've added an ID property, which will allow us to connect to the sonnet by number. Which is different than the title property, which is the sonnet number in Roman numerals. We've also added an OtherLines, a FirstLine property, which is the first line that shows up in that list box, and an OtherLines property, which is all the rest of our sonnet. And then finally, a Boolean to say whether it's a favorite or not. So, the way we're going to do this in the flat file version is we're actually going to read and write all 154 sonnets to and from XML.

And when we want to create our list of favorites, we'll be looking at the Boolean flags iterating through the list of sonnets to create a subset of our list. Save that. Now, we're going to come and add some code into the main app C# file. So, look at what we did here. We first of all added some additional using statements, or System.IO, System.IO.IsolatedStorage, and System.Xml.serialization. That's, so that we can serialize the data back and forth using those stream readers and writers and text readers and writers is why we need the System.IO.

And the System.IO.serialization is so that we have serialize and de-serialize against the XML model. So, the name of our "database" here in isolated storage is going to be SonnetsWithFavorites.xml. And, in the app constructor, after we go through all the standard template stuff, we then say we want to look and see if the "database" file exists. And if not, we're going to prepopulate it with the contents of that ShakespeareSonnet.xml which is in a format that is not exactly what we want to use in this app. And so, instead of just reading that and creating the view module each time, we're actually going to do it on a one time basis.

Read that and create instances of our sonnet class so that we can shuffle it back and forth. Isolated storage is essentially all the read write file system that exits on the Windows Phone device for you to save and, and load files. The reason it's called isolated storage is because the set of files and folders. For each app is isolated in a sandbox so that all of your files in sonnets plus are separate from all of the pictures in the picture hub, are separate from all of the mail stored by the mail application.

There is no way for one app to see the contents of isolated storage for another app. In order to get access to the isolated storage directory for your app, you have to use this IsolatedStorageFile.GetUserStoreForApplication. So we go and get that, we do it in a using statement so that it actually gets disposed of at the end of the access to isolated storage. Because the isolated storage implements disposable, and this is a good way to have your app not leak memory. So, we go get access to the user store for application, that's the isolated storage.

And from there we can create files, delete files. First, think we're going to do is use file exist to see whether or not our database file exist out there because we only want to do this once. So, we look and see if it exists. If it doesn't, we're then going to go and create a file. So you use OpenFile, pass in the name, and use System.IO.fileMode.Create, and you get a stream. Then, we have to have some data to put into that stream, and so we have a modified version of the method we had in our view model before that loads that the Shakespheresonets.xml file into a observable collection of item view model instances.

We'll look at that in just a second, but that essentially looks like what our view model looked like before. And now, we have this observable collection of item view model instances and we use the standard XML serializer techniques to write it out to the file. We create a serializer from that, create a text writer from the stream, serialize it out and close everybody down. Now, we have this file in isolated storage that anytime we need to load up the data to put into a list box or to add a favorite or delete a favorite we can use that file. This low database from XML, as I said, is essentially identical to the implementation we had in the main view model of the basic Sonnets app. The difference being instead of taking the items and adding them to the items collection of the view model, we create a new observable collection of the items. And at the end, as we create each one, we simply add it to that items list. We also go through and prepopulate the two new things that we've added to the view model, the sonnet ID and the, it's favorite property. We set it's favorite property to False.

Remember, this is happening only when we're loading the static data from ShakespeareSonnets.xml. So, we don't have any favorites yet. And we also set up the sonnet number, so that we can access things in this list by index. So, now we've looked at the solution that we've got. We added the sonnets class so that we have a place to put our data that we're going to store in isolated storage. And we've modified the app.zemble.cs file to include a 1 time code, to transfer our static data from the ShakespeareSonnets.xml file into our file and isolated storage so that the rest of our app has a known state to begin.

Show transcript

This video is part of

Image for Windows Phone SDK Essential Training
Windows Phone SDK Essential Training

65 video lessons · 3457 viewers

Michael Lehman
Author

 
Expand all | Collapse all
  1. 2m 34s
    1. Welcome
      55s
    2. What you need to know to take this course
      51s
    3. Using the exercise files
      48s
  2. 15m 44s
    1. Downloading and installing the Windows Phone tools
      2m 44s
    2. Creating a Hello World application
      9m 31s
    3. Debugging on a Windows Phone device
      3m 29s
  3. 6m 29s
    1. Exploring device capabilities
      2m 58s
    2. Understanding the Emulator
      3m 31s
  4. 8m 51s
    1. Understanding the SDK
      8m 51s
  5. 41m 32s
    1. Introducing the converter
      3m 36s
    2. Creating the user experience (UX)
      13m 35s
    3. Responding to the Application Bar
      2m 40s
    4. Implementing click handlers
      3m 52s
    5. Saving and loading settings
      8m 18s
    6. Preparing your app for shipment
      9m 31s
  6. 20m 48s
    1. Introducing sonnets
      1m 38s
    2. Data binding with Silverlight
      4m 4s
    3. Exploring the Model-View-ViewModel pattern (MVVM)
      4m 2s
    4. Implementing the Master-Detail pattern using pages
      3m 53s
    5. Loading external data
      7m 11s
  7. 50m 26s
    1. Choosing a multipage controller
      3m 13s
    2. Preparing for persistence
      7m 30s
    3. Updating the data model
      7m 2s
    4. Creating the details page
      4m 30s
    5. Bringing it all together
      3m 58s
    6. Cloning a Windows Phone app
      6m 49s
    7. Using SQL CE
      8m 49s
    8. Updating the data model
      2m 24s
    9. Querying and updating the database
      6m 11s
  8. 14m 19s
    1. Working with the camera
      6m 11s
    2. Exploring GPS
      5m 21s
    3. Exploring the accelerometer
      2m 47s
  9. 29m 34s
    1. Introducing recording and playback with XNA
      45s
    2. Capturing sound
      5m 12s
    3. Providing feedback while recording
      5m 50s
    4. Playing a recording
      4m 34s
    5. Persisting a recording
      2m 58s
    6. Listing recordings
      5m 15s
    7. Managing the recording list
      3m 26s
    8. Recording under the lock screen
      1m 34s
  10. 28m 32s
    1. Building the UX
      3m 54s
    2. Using WebClient
      4m 1s
    3. Determining network connectivity
      2m 9s
    4. Leveraging Internet Explorer
      7m 56s
    5. Adding a live tile
      2m 42s
    6. Building a background agent
      7m 50s
  11. 14m 46s
    1. Hello World in C++
      4m 35s
    2. Libraries
      4m 17s
    3. Managed plus managed
      5m 54s
  12. 26m 35s
    1. Exploring built-in controls
      4m 28s
    2. Windows Phone Toolkit
      2m 42s
    3. Introducing Coding4Fun
      3m 13s
    4. Reminders and notifications
      6m 10s
    5. Wallet and In-App Purchase
      1m 34s
    6. File and protocol associations
      1m 59s
    7. Bluetooth
      44s
    8. Integrating speech
      1m 28s
    9. Near field communication (NFC)
      1m 23s
    10. WinRT and legacy Windows Phone APIs
      2m 54s
  13. 12m 14s
    1. Publishing your apps
      2m 49s
    2. Learning from apps in the Windows Phone Marketplace
      1m 22s
    3. Staying in touch with updates and new information from Microsoft
      2m 34s
    4. Looking at Windows Phone 7 app reviews
      44s
    5. Exploring web sites books and other useful links
      4m 45s
  14. 1m 34s
    1. Farewell
      1m 34s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Windows Phone SDK Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.