Windows Phone SDK Essential Training
Illustration by Don Barnett

Updating the data model


From:

Windows Phone SDK Essential Training

with Michael Lehman

Video: Updating the data model

Okay. We've got our raw data transferred from our solution from our ShakespeareSonnets file into our IsolatedStorage.xml "database". Now we need to change the data models to be able to read and write that and create the things that we need to have for our Pivot control. But before we get into that let's take a brief look at what a Pivot control needs in terms of data. So let's open up the Main XMAL, reformat and what we're looking at here is the raw pivot data from the template, but it will give us a way of looking at what the structure of it is so we understand what we've got to do.
Expand all | Collapse all
  1. 2m 35s
    1. Welcome
      55s
    2. What you need to know to take this course
      51s
    3. Using the exercise files
      49s
  2. 15m 45s
    1. Downloading and installing the Windows Phone tools
      2m 44s
    2. Creating a Hello World application
      9m 32s
    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 49s
    1. Introducing sonnets
      1m 38s
    2. Data binding with Silverlight
      4m 4s
    3. Exploring the Model-View-ViewModel pattern (MVVM)
      4m 3s
    4. Implementing the Master-Detail pattern using pages
      3m 53s
    5. Loading external data
      7m 11s
  7. 50m 27s
    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 12s
  8. 14m 19s
    1. Working with the camera
      6m 11s
    2. Exploring GPS
      5m 21s
    3. Exploring the accelerometer
      2m 47s
  9. 29m 35s
    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 35s
  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 36s
    1. Exploring built-in controls
      4m 28s
    2. Windows Phone Toolkit
      2m 42s
    3. Introducing Coding4Fun
      3m 14s
    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 15s
    1. Publishing your apps
      2m 49s
    2. Learning from apps in the Windows Phone Marketplace
      1m 23s
    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 your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course Windows Phone SDK Essential Training
4h 33m Beginner Jun 11, 2013

Viewers: in countries Watching now:

Discover how to build professional apps that encompass the 17 major feature areas of Windows Phone, from XAML to multimedia to network access. In this course, author Michael Lehman details the standard hardware device configuration, teaches how to navigate the development environment, and explores the Windows Phone APIs. The course shows how to build sample applications while learning Windows Phone concepts and frameworks, including the Metro design language.

Topics include:
  • Downloading and installing the tools
  • Understanding the SDK
  • Designing the user experience (UX)
  • Implementing commands
  • Data binding with Silverlight
  • Exploring the Model-View-ViewModel (MVVM) pattern
  • Loading external data
  • Capturing data from the camera, GPS, compass, and accelerometer
  • Recording and playing sound
  • Implementing persistence with isolated storage
  • Leveraging built-in tasks, choosers, and launchers
  • Expanding the available controls using the Silverlight and Coding4Fun toolkits
  • Cloning a Windows Phone app
  • Learning how to publish your app
Subject:
Developer
Software:
Windows Phone
Author:
Michael Lehman

Updating the data model

Okay. We've got our raw data transferred from our solution from our ShakespeareSonnets file into our IsolatedStorage.xml "database". Now we need to change the data models to be able to read and write that and create the things that we need to have for our Pivot control. But before we get into that let's take a brief look at what a Pivot control needs in terms of data. So let's open up the Main XMAL, reformat and what we're looking at here is the raw pivot data from the template, but it will give us a way of looking at what the structure of it is so we understand what we've got to do.

So here we've got in our controls, we don't have a grid at the top with our name of our Application and the Title of the page, instead we use the Pivot control that's where the application goes, and there are two PivotItems, one for each page. Now obviously you can have more than two, but for Pivot, you usually have at least two. So let's take a look at what it's inside of a PivotItem. Inside of a PivotItem here we have a ListBox that doesn't have to be what's in every Pivot, but that happens to be what we're doing because we want two Lists, one for all sonnets and one for the favorite sonnets.

Again this ListBox in this case is bound to the Items property of our MainDatamodel and similar to the Windows data bound application that ListBox.ItemTemplate has a StackPanel in it with LineOne and LineTwo. Now if we go look at PivotItem number 2 in the default text that comes out of that of the template, again they're binding to the Items collection in the MainDatamodel. When we look at our actual Pivot control, you'll see we're going to binding to a separate Items collection that way we get two different views over the top of the same data.

Now let's go to the MainViewModel and make some updates there. Let's take a look at what we did here. Again we added some code to access IsolatedStorage because here is where we're going to go load that SonnetsWithFavorites.xml and also write it back when the user taps the star icon in the application bar. Because we now have two lists in our Pivot, and we look at the actual structure of the pivot just before we run the final app.

But we need two lists in our ViewModel, because we need one for the ListBox of all items and one for the ListBox of FavoriteItems. So we created two ItemViewModel ObservableCollections, we created an additional property in order to be able to retrieve those FavoriteItems. This is all the same for the ItemsProperty, but we also added a new property to go along with the INotifyProperty interface so that when we update the Favorites, the list of favorites automatically gets updated. We saw that in run-through of the app that when we clicked on the star and then used the Back key to go back to the list of favorites, it automatically was there, we didn't have to do any code in order to be able to get that Favorites updated because of the Silverlight data binding.

And now our LoadData is different because instead of loading from the ShakespeareSonnets.xml, we're not going to load from our IsolatedStorageFile. So we do something sort of in reverse order for what we did for saving it. We go create an ObservableCollection to get all of our sonnets. Once again we go GetUserStoreForApplication to get access to the IsolatedStorage for this app, and then we create an IsolatedStorageFileStream in order to read it, but in this case, we use OpenFile and use Filemode.Open. And then similarly we create StreamReader instead of a StreamWriter, another serializer, and then we call Deserialize to load the data in.

Once we've done that, we now have this ObservableCollection of ItemViewModels and what we're going to do is go through and populate our two lists. If you remember, we have two lists of items in our ViewModel. One is our main items list and the other is just a list of Sonnets that are favorites. So we basically add everything to the main items list and then look at the Favorite flag as we are iterating over them to decide if it should be added to the Favorites List. The reason this has the red squiggly underneath is because we haven't yet updated the ItemViewModel which is the next thing we are going to do.

So this will automatically resolve itself. And finally, we added two more methods here which are called from the Details page when the user taps the star icon to set a favorite or delete a favorite. So at that point, we'll see that when we look at the code for that, the user taps that, it comes back here to the MainViewModel and says either add this particular item to Favorites or remove it from Favorites. And it simply uses the built-in collection mechanism to add or remove, and because when we loaded the data from the XML file, we took the same instance of the ItemViewModel and put it into both lists. We can use these methods because essentially even though we might have a list of all items with all 154 sonnets in it and a list of favorite items with three sonnets in it.

Those lists are not two separate copies of say sonnet number three, they both effectively point to the same instance of an ItemViewModel class that is sonnet number three. Because we're implementing an instance of INotifyPropertyChanged, we have to call NotifyPropertyChanged to indicate that the favorites have changed, and that's how the list gets automatically updated. Finally, the very last method is how do we actually update the database when somebody makes a change? So the code in the Details page, which we'll see in just a minute that handles the tap on the star icon calls one of those two methods to add or remove from the Favorites list and then calls UpdateDatabase.

Now UpdateDatabase is essentially the same as what we did in the main app constructor with one caveat which is that we delete the file first and then write it all over again. So we're not actually updating the XML file in place we're simply writing it completely over again. So while this is a reasonable thing to do for 154 Shakespeare Sonnets, clearly this would not scale to thousands of items. But we simply delete the file, create it over again and write it back, and that's over changes to the MainViewModel and one more change to the ItemViewModel to add a couple of properties, and we'll be ready to start actually looking at what the changes are to our Details page.

So let's open up the ItemViewModel and add these two properties and the two properties are called SonnetId and IsFavorite, and because we implement INotifyPropertyChanged, we have to as we do each set call NotifyPropertyChanged for these particular properties. So we've now taken our basic structure, we've added a class of Sonnets.cs to hold the data for our XML file, we've made some changes to the App.xmal.cs to initialize the XML file from the ShakespeareSonnets.xml on a one-time basis, and we have made changes to the MainViewModel and the ItemViewModel in order to support two lists in the MainViewModel and in order to support the additional properties we need for the ItemViewModel namely the SonnetId and the IsFavorite property. I think we are ready to go.

Let's go on now and build the Details page and get ready to make this app save our favorites.

Find answers to the most frequently asked questions about Windows Phone SDK Essential Training .


Expand all | Collapse all
please wait ...
Q: This course was updated on June 10, 2013. What changed
A: This course was updated on June 10, 2013. Revisions include:
  • Over two dozen movies revised to reflect changes to Windows Phone 8 (released October 2012)
  • New instructions for downloading and installing to Windows Phone toolset
  • New instructions for working with Windows Phone 8 device data
  • New chapter on native development with C++
  • New chapter on advanced Windows Phone features including in-app purchasing with Wallet, reminders and notifications, Near Field Communication (NFC), and more.
 
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.

* Estimated file size

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 ?

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.