Udostępnij za pośrednictwem


Starting Windows Phone Development: Lesson 2 of n - Access to Application Isolated Local Storage

I hope you will find this lesson more interesting as most developers think about phone storage available on devices for their applications and how they can utilize it. Each application will have its own isolated local storage. It is independent from the other applications and OS files and can be only accessed by your application.

Before we move on, you need to have a basic understanding of event handling. I went through this in Lesson 1. Please finish it first before moving on.

Two questions would pop in your head, one would be about utilizing storage for your app on an SD card, and the answer yes and the next question would be about the size limit, so as I know (and I know little Smile) you will have virtually no constraints on the application storage capacity. So as long as you have storage available in your device then your app can utilize it.

The idea of our application is very simple, I will use the local storage to read/write a string message and a counter to a text file. I will be increasing the counter every time we launch the application (aka load the page) and will display the last message stored in the text file.

You can always find the lesson source code attached at the end of the blog post.

So, How does it look like?

image

As you figured we used 2 TextBlocks to display the message and the counter and one TextBox to capture the new message if any. So what about the XAML behind this, well as always StackPanel Winking smile is my hero:

image

In this scenario we named the TextBlocks because we will be pushing content to them from our code behind. So lets start with what happens when the page loads.

So what happens when the page Loads?

Before we start with the page load logic, please make sure to add the following using statements on the top of the page to be able to use the isolated storage and carry on file operations.

image

On page load I need to check if the file I use to store the string and counter exists or not. If it does exist I read the counter and the message and push them to global variables. If the file does not exist, I write 0 and “No messages..!” in a new file. The file I will be using will be called LS.txt (short for Local Storage), I know very impressive naming skills Smile with tongue out.

Finally, I push the values to the TextBlocks by replacing the “##” symbols in my string with the variable values using the string.Replace function.

image

and when it Unloads?

when the page unloads or navigated away from using the Back or Start buttons, we replace the current LS.txt file with the new content. First we increment the counter and then add the message.

image

The final part of our application is saving the new message and exiting the application so that we call the OnNavigatedFrom handler.

Save New Message

We push the contents of the TextBox back to our variable and push a message box to the user.

image

Let’s Test it out..!

Run the emulator using the F5 key and please be patient until Visual Studio manages to compile, build, and deploy our application.

The application loads with the counter set to 0 and I input a message.

image

Now I will hit save and the application will display the message box. We will exit when the message is dismissed which will call the unloaded and increment the counter and save my new message.

image

Once you hit back after dismissing the application, go to the menu again on the phone and click on Lesson 2.

image

and taraaaaaaaaaaaaaaaaaa..!! Smile we have an updated counter and our last message:

image

I hope enjoyed your lesson today and have an idea on how to read and write files in Windows Phone.

Let me know if you get stuck somewhere.

Lesson2.zip

Comments

  • Anonymous
    January 14, 2013
    I'd love to see all of these posts on starting Win Phone Development available as a series, perhaps through a tag, or something, so that I can share it on our company's wiki. I'm sure others will want to be able to see just this series, also, as they go through it. Great work, thank you!
  • Anonymous
    January 15, 2013
    keep it up!
  • Anonymous
    January 15, 2013
    This is very helpful; We have an upcoming project on windows phone 8; I have to get started with development really soon but I don't have the time to refer to a book; This series provides quick tips on what is really needed to get someone started swiftly. Thank you for directing myself on the right track :)
  • Anonymous
    January 15, 2013
    @Misty Fowler,Just created a new tag called: Starting Windows Phone Development Series and tagged both lessons. I am glad that you want to share this with your peers. I am working on the third lesson now and its coming up very soon. Please let me know if you have any ideas/topics that you want me to tackle.
  • Anonymous
    January 15, 2013
    @Huber,Glad to have a real fan =) third lesson is coming soon thank you for following up.
  • Anonymous
    January 15, 2013
    @WPRanger,I should have asked you to write the series introduction, thank you for the kind words. Please let me know if you are looking for specific topics.
  • Anonymous
    January 17, 2013
    Great, thanks for adding the tag. Keep up the good work!
  • Anonymous
    March 21, 2013
    I am a fan tooo, you should write a book or prepare HOL,
  • Anonymous
    November 02, 2014
    Hello Guys,I want to save image in Local Storage.How to do this.Please help ASAP.
  • Anonymous
    November 02, 2014
    @AjitYou can think of converting it into a stream and store it as an array of bytes. More context will be useful e.g the source of the image and what are you going to use to retrieve it, etc.