Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you remember back to our original sonnet set, we had a details page, but all it allowed you to do was look at the sonnet, and the only way you could interact with it was by clicking the Back button to go back to the list. Here we need a details page that not only has the XAML for putting the text of the sonnet on the screen. We also need some code behind that actually influences the functionality of the toggle favorites button, and so we're going to add a new details page to our solution. Also note that we created this solution using the pivot application template, which gave us only a main page and not a details page.
It's not every pivot application has a details page. But here we are going to add a new item, I am going to put a Windows Phone Portrait Page. I am going to call it DetailsPage, and in XAML what we're going to have in there, looks like this. It has our title for our application. The text block at the top now has the word sonnet, because we're also going to put the sonnet number in there. We also have an image control, that you can see by default has an empty string for the source property which makes it transparent.
And when the sonnet has a favorite, will put images/favorites.png, to make our gold star show up. Lastly, we've added down here in the ApplicationBar, a menu item with that appbar.favs.rest.png image and also the Text of Favorite and a Click-handler called toggleFavorite, which will now go and add to the code. So let's take a look at what the code behind our DetailsPage is.
So we can now see what it is we need to do. So, as before we have our ItemViewModel for the item, and we have our OnNavigatedTo. But now our OnNavigatedTo method needs to do a little more. First, we go to see if we have a selectedItem, which we always should have. Then we parse it back into an integer. We are going to see if we have another query item that we add to the code that handles selection from the favorites list or the all list, which recalled from favorites, and we go to see if we've got that.
So that we know that if the fromFavorites is set to 1,that we need to extract from our FavoriteItems in the ViewModel versus the regular items in the ViewModel. Once we've got our item we also keep track of the sonnet number, and finally we need to look at the IsFavorite property of that item that we just got to determine whether or not the URI for the image should be Images/ favorite.png or it should be left at the empty string. The second thing we've added here is this toggleFavorite_Click method, which handles the event when the user taps on the star in the ApplicationBar.
First, we'll see that we have a Debug. WriteLine here, as we were building this app we wanted to know that we actually got the right one. And as you probably already know the way to get access to that debug stuff is to add System.Diagnostics to your usings, and then you can use this, and it will come out in the output window of your Visual Studio as you're running the app. So what toggleFavorite does is it goes, gets our current item, toggles the state of that favorite boolean, and then we look at what is the new state of it.
So if it means it's now a favorite, we set the image source of our favorite image to the favorite PNG so that the gold star shows up. And then we go back to the MainViewModel and call AddItemToFavorites. So now this item will get added to our Favorites collection, and because that's databound to the list box in the Favorites section of the pivot, it will automatically get updated. Similarly, if it's no longer a favorite, we falling out in love with that particular sonnet. We set the FavoriteImage.Source back to the empty string, and we go back to the ViewModel and call RemoveItemFromFavorites so that it gets removed from that list, again data-binding will take it out of the actual list box.
And either way, whether or not this is a new favorite, or you know an old song that we no longer care about, we call UpdateDatabase which will take our collection of sonnets along with its favorite flags and write it back out to the isolated storage. Next step, we're going to make the changes to our main page to actually put our version of the lists into the pivot control, and then we will be ready to run.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101260 Viewers
61 Video lessons · 88019 Viewers
71 Video lessons · 71883 Viewers
56 Video lessons · 103720 Viewers
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.
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.
Your file was successfully uploaded.