Posts Tagged ‘ Isolated Storage ’

Working with Isolated Storage, ListBox and DataTemplates

In my previous blog entry I was talking about how to write files to and read files from Isolated Storage. For this entry I want to dig a little deeper, for example I want to display a list that has been created in Isolated Storage. Each file in the list is going to be displayed in a hyperlink, so that I can click on it and then open up a second XAML page that will actually display the content of that file.

Let’s get started. I’ve created a new project and first of all I am going to add a ListBox that will list all of the files:

Within that ListBox I want to create something that is called Item Templates. So, I am going to create a new section called ItemTemplates. This is going to hold a definition for what every row in the list should look like. In this specific case I am going to create a DataTemplate, because I want to merge data together. Inside of this DataTemplate I am going to put a HyperlinkButton:

When the user clicks the fileLinkButton an event is fired off. I am not going to put a navigateUri for each item from the list into the XAML, instead I am going to capture the click event, then construct one and then pass it to the second page that I am also going to create at runtime. I am handling this event in the code behind.

In addition to the ListBox I am going to create a button. The button’s purpose is to load a series of three sample files into Isolated Storage:

The next thing I am going to do is to add a second page:

Now I am going to the code behind, I am navigating to my button click event handler and add some code:

This is the code to load the first sample file. Considering I need three of them, I better create a little helper method to keep my code as simple as possible:

So far, so good. But just creating those examples is not going to be enough to populate the ListBox. I have to take care of that myself. And I need to do that in two places.

First, whenever a sample file is created I want to make sure that it is bound into the ListBox. But also when the application first loads, I want to display the samples. In Order to do that I simply create a loaded event using the properties window. Then I am going to create a helper method called bindList where I am going to data-bind the names of each file in Isolated Storage. I am going to bind them to the ListBox:

Now that I have created the bindList helper method I want to put that into the loaded event and call it from my PhoneApplicationPage with that loaded event:

The line “bindList();” I now have to also put into the loadSamplesButton click event, immediately after where the files are created.

So, let’s have a quick look at my code. First whenever I click the loadSamplesButton and call the createSample helper method, this will create a new file with the defined fileName and fileContent to Isolated Storage. I am also making sure that the file doesn’t already exist in which case I wouldn’t want to overwrite it. If it doesn’t exist I create it. Then I am using a StreamWriter passing in that file reference, and then finally write to the file whatever was passed in to my helper method.

I create three files and then I call that bindList. What bindList does is simply going to appStorage and getting all the names of files that are in appStorage and stores them in an array of strings. Now that I have retrieved the fileNames I want to data-bind them to the ListBox.

Let’s see if it runs:

Nice, it just works. Now I have to make sure that the HyperlinkButtons work. With right-clicking on that fileNameLinkButton click event I can navigate to the event handler to take care of that action.

What I am going to do there is to construct a Uri on the fly and then pass it to that part of the Windows Phone API that knows how to navigate from one page to another. I am going to make use of a navigation service instead of the simple HyperlinkButton NavigateUri property:

I am just continuing to my SecondPage XAML code to add some controls there:

I have a TextBlock to display the content of each clicked HyperlinkButton and also a HyperlinkButton that is navigating me back to my MainPage. For this to work I have to create a loaded event here, too. Inside that loaded event I want to look for the file name, and then I am going to look on the flash drive of the phone in its Isolated Storage to retrieve the values that are inside of the passed in text file:

Let’s see if it works:



When I click a HyperlinkButton I am navigated to my SecondPage where the content of that just clicked HyperlinkButton is being displayed. Clicking on  the HyperlinkButton on the SecondPage brings me back to my MainPage. Just how it is supposed to work.


To be continued…



Understanding Isolated Storage

So far in my examples I allowed the user to input some information, but I’ve not done anything useful with that information.

So, today I am going to use the phone’s own storage capacity, its Flash drive to store the information.

In Silverlight applications I don’t get free access to the entire phone’s file system. The user is protected from portentous, malicious applications. And it does that by providing each application with its own personal storage area. That is called Isolated Storage. That makes sense, because each storage area is isolated from its ‘neighbor’ in applications.

So, how do I store to and retrieve data from Isolated Storage?

I am just creating a new project and I am going to add a few buttons and a textbox  into the content panel:

Now I am going to add some code to the event handler that is going to address the Isolated Storage. It is going to create a file and save, whatever the user typed into the textbox, into that file:

First I am creating a new instance of IsolatedStorageFile by creating a new variable called appStorage. GetUserStoreForApplication is going to retrieve a reference to an object that will allow me to interact with this specific application’s Isolated Storage area. “var” is a special keyword, called Local Type Inference. It allows C# to assign the data type when the application is compiled based on what the variable is initialized to. C# will figure out the data type that is returned from this method, GetUserStoreApplication, and create a reference, a variable called appStorage.

GetUserStoreForApplications returns a reference to this application’s Isolated Storage space, so that I could write and read files from it.

With the next line (string fileName = “simple.txt”) I am just hard coding the name of a file. This will be the file name that I am going to store on the phone.

The next line looks a little bit more complex:

using(var file=appStorage.OpenFile(fileName,System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))

I am telling C# the file name and the FileMode, in other words, I am telling C# how I want this file to be treated. If it exists, open it, if not, create it. And finally I am giving permission on what activity I want to perform on that file. In this case I am allowing access that data can be written on that file.

The keyword “using” in that context forces the file that I am working with (inside the open and close parenthesis) to close once the end of the code block is reached. This reduces the possibility that I accidentally leave that file open and accidentally corrupt that file somehow when I try to open it a second time and write more information into it. So, “using” defines a code block, and once the CLR is finished executing that code block, then the file will be closed automatically. Basically, what happens is, that reference (var file) will be set to Null, it will go out of scope and it will be removed from the phone’s memory.

In the next line of code I am creating a reference to a StreamWriter object:

using (var writer = new StreamWriter(file))

This is an object that knows how to write data into an open file.

Ok, let’s move on and add some code to the Clear button:

This just cleans out whatever text is already in that textbox.

Now I want to add some code to the Open button. The idea is, now that I’ve saved some data to the Isolated Storage I want to retrieve that “simple.txt” file from Isolated Storage and then display its content into my infoTextBox:

First I am getting a reference to the application’s Isolated Storage area. Then I am using a StreamReader to get a reference to the “simple.txt” file, and open it up for read only access. And finally I am using the StreamReader’s ReadToEnd method to take care of the details of retrieving the data from the “simple.txt” file, so that I can display it in the infoTextBox.

When I run it now, I can type in some data (1), I can save it (2), clear it (3), and by clicking the Open button (4) retrieve that data again:

Retrieving the data back by clicking the Open button would not work anymore if I would close down the emulator and start a new session. It only works for the current session.

To be continued…

%d bloggers like this: