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

Loading external data

From: Windows Phone SDK Essential Training

Video: Loading external data

It's time to stop looking and start writing. Let's bring in our ShakespeareSonnets and write some code to be able to put it on the screen. The first thing we do is come to the Solution, come to the Sonnets project and Add a New Folder, and we will call it Content. Inside the Content folder, we will Add an Existing Item for those of you with the exercise files, that's going to be in the Sonnets_End folder for this chapter, and it's called ShakespeareSonnets.xml.

Loading external data

It's time to stop looking and start writing. Let's bring in our ShakespeareSonnets and write some code to be able to put it on the screen. The first thing we do is come to the Solution, come to the Sonnets project and Add a New Folder, and we will call it Content. Inside the Content folder, we will Add an Existing Item for those of you with the exercise files, that's going to be in the Sonnets_End folder for this chapter, and it's called ShakespeareSonnets.xml.

Similar to the data we used for units converter for images and so forth, we have to make sure that this is content, so when we go the Properties page, in this content but we need to make sure it's says Copy always. So it's actually gets included into our build. We are done with properties, and now to actually make this run, what we are going to do is we are going to come down to the MainViewModel and look at its LoadData method. So we come down here, and we see the LoadData method that came from the template which puts scan data into the view model.

We want to put real life Shakespeare data in it, so we are going to select comments ahead of that method and entire LoadData method, delete it and put our code in. Let's take a look at what this does. What it does is load the content of our ShakespeareSonnets.xml into an XDocument, and you can see that the XDocument has a little red squiggles underneath it which means we need to add a new name space. So let's go the top and type using System.Xml.Linq.

you see from the red squiggle under Linq that we don't have the proper assembly so let's go to the Solution Explorer, right-click on References, select Add Reference click here to sort the list by name, and when it's in ascending order, we will come down to the bottom, and we will find System.Xml.Linq. We will click OK and red squiggles go away which means we have that code that we need. So now let's scroll back down to our LoadData method. So we load the contents of the ShakespeareSonnets.xml file into a XDoc.

Let's take a look what that file looks like. Each sonnet has a number, an id, it has a Roman numeral letter, and it has a body, and that goes on for all 154 sonnets. So in order to load this into our data structures, what we do is select the Sonnets Descendants so that we get all of the sonnet data, and we are going to do for each loop, creating a new instance of ItemViewModel for each sonnet. We create the instance, we set the LineOne property equal to the Roman numeral character of the sonnet that we extract from the XML.

Then we are going to go through, and iterate through each line and the reason that we are going to do that is because we need to do two things and the LineTwo property of the Item view model, we want to put first line of the sonnet, and then the line 3 property of the ItemViewModel, we want to put the entire sonnet with lines 13 and 14 indented. We have a counter to know which line we are on, we have a string so we can accumulate the body, and we go get each one of the line elements inside the body and do before each over that.

We say, it was the first line, we set the LineTwo property because that's going to show up on our master page where we have the roman numerals and the first line of the sonnet, and then we say if the LineNumber is less than 13, we simply put the line in as it is if the line is 13 or greater, we put in the line and an extra of couple of spaces in order to indent those last two lines. Finally, when we are done iterating over the lines, we set the LineThree property of our ItemViewModel. Now at the end here, we now have a new ItemViewModle instance, and we added to the items collection for this page so that we see all the items in the main page list box and so we also have those two extracts, remember over in the details page we looked at the fact that it comes back to this items property and extracts the selected one to show on the details page.

So let's go ahead and give this a try and voila, we have 154 sonnets, and if we come down to our favorite sonnet and click on it, there we go, Shall I compare thee to a summers day? Right there in front of us. Let's exit the app, and we need to do the same two things here that we did at the end of the unit's converter. Let's add the above box, and let's customize the icons. We will come here, we will say add new items, select Windows Phone Portrait Page call it About.

In this case we are going to use the same code snippet we used for units converter and change the title, like up here let's say SONNETS, over the Solution Explorer and open the About code and the same thing, you will select it, we will use the code snippet, change the title of the app Let's say SONNETS and change the namespace here. And finally, we will come over to the MainPage.xaml.

Now so far we haven't have any application bar going on in this particular thing so we are going to come down here, and we are going to uncomment the application bar xml that's in the template. We can do that by coming to Edit > Advanced > Uncomment Selection. We are going to delete the ApplicationBarIconbutton elements because we don't have any of those, and we are going to delete the two template menu item ones and twos, and then add our codes in here.

Change this to the to say About sonnets. And finally, we will come to our MainPage.xaml, go to the bottom and add the method which we actually invoke the About box, and we are good to go. Let's give it one last run. One of things that can happen to you when you copy, code, end our xaml from another projects and put it into a new project. What happens is that our About.xaml up here has the name of our namespace for our Class.

So for instead of UnitsConverter, this needs to be sonnets. Some of this is going to happen to your many times as you move things around between projects. I know it is the name space in the code, have to be correct. The name space in the xaml has to match. Now if we try to run this Right now, we would end up with some compile errors because we change the xaml let's go over to the Solution Explorer and right-click here and say Rebuild Solution because that will allow xaml and the code to get in sync. So the compiler knows the two of them are together.

So now when we run it, we will deploy off here to the emulator, it was our sonnets, here is our About box, and when we come back, we can look and see there is our new icon, which is the roman numeral for 154. Now we are done with this app, and this is what we are going to do with these one, we will pin it to the Start menu. So there now we are now done with app number two, here in Windows Phone SDK Essential Training.

Show transcript

This video is part of

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

65 video lessons · 3306 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.