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.
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 .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.