Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Microsoft Silverlight 5 is a rich application framework for creating high-performance, cross-platform desktop and mobile applications. In this course, author Walt Ritscher demonstrates how to build a variety of applications in Silverlight, with particular focus on building compelling business applications and delivering premium video and audio content. Developers will work with the C# programming language and Visual Studio Professional, as well as Expression Blend, a tool that simplifies creation of the interactive user interfaces expected in modern-day applications.
In this movie, I want to talk about storing data on the local computer; more explicitly, I want to show you how to store data in local files. The location for the files depends on several criteria. If you can get the user's explicit permission via the Open File dialog or the Save File dialog, your Silverlight app can write to local stories just like any other desktop application. Application files are commonly stored without a user prompt, however. Let's say you need to make a temporary file to store the audio stream from the user's microphone.
This temp file serves as a buffer or backup for the recording session. Saving this data into a file will happen without user intervention. Most operating systems restrict where an app can save files, and the operating system takes special precautions with web applications. Granting web applications unfettered access to the file system is sure to put your computer in jeopardy. To solve this dilemma, Silverlight uses isolated storage. Isolated storage provides a safe isolated file area.
By isolated, I mean that it is set aside from the normal user file path. Isolation also means that each Silverlight application gets its own secluded data store. So to summarize: you can store data and files on the local computer, either directly to the local hard drive with the user's permission or you can store to the hard drive without the user's permissions, which you can only use isolated storage. There is one other exception to the rule. If you run your application with elevated trust, you can access the users' documents folders--like their music and picture folder--without user involvement.
For this demonstration, I'm going to use a Visual Studio. I have opened a solution called StoringDataOnTheClient. This solution contains two projects: RememberingUserSettings and StoringDataOnTheClient. What I would like to do with this remembering user settings is to remember which one of these tabs the user has selected when they leave the Silverlight application. To do that, I had to write a little bit of code. Here I am wiring up an event handler for the TabControlSelectionChanged event.
When the user picks a new tab, my code will run in this method. One of things I like about Silverlight is that application-level settings are automatically stored in isolated storage. All I need to do is go to IsolatedStorageSettings.ApplicationSettings and provide a key value and then assign my piece of data. In this case, I am assigning the SelectedIndex of the tabControl. This FavTab is coming from here, this constant string value. Once, I put the information in there, I call save to persist to the hard drive.
Notice that I didn't have to check if the file exists first; Silverlight handles that for me. When the user comes back to my Silverlight application, the MainPage_Loaded event will fire. What I'm doing here is verifying that the application settings contains my information. If it is there, then I go in and get the information I stored, which was in integer value, and I place it in this variable. Once it's in the variable, I then assign it to this SelectedIndex of the tabControl. Let's see this in action.
Press F5 to run the application. I select the Adventure tab, and then I quit the application and restart it. Success! I'm now on the Adventure tab. Next, I'd like to talk about saving your own files, not just application-level settings. To do that, I'm going to go to the StoringDataOnTheClient project. I'm going to right-click on it and make it the StartUp Project, so that the next time I run the application it will use the correct project.
And then I'm going to go down and look at MainPage.xaml. My first demo is going to look at how you might save information into isolated storage. In this example, I am simulating the user clicking on the Save button. I'm going to come down here and get the location for the IsolatedStorage. I don't know the name of the directory where my files are stored, so I have to go to Silverlight and ask for the application store. That's what I'm doing here, GetUserStoreForApplication. So it's per my application, and it's per each user that is using my application.
Next, I check to make sure the directory in the isolated store exists. And if it does, I check for a file. If the file does not exist, then I create the file and then once I create the file, I do my normal file saving work here. Later, when I want to retrieve the information, I get the UserStoreForTheApplication, verify that the Directory and the FileExists, open the file, and then do my file retrieval. Remember, I said you could store in other locations as long as you ask the user for permission.
So let me show you how you would do that. Here is a button that's going to save to the local hard drive. I do that by creating a SaveFileDialog, setting a few properties, and then showing the dialog. If the user clicks OK, this will be equal to true. Next, I create a stream from the file, I create a writer to help write the information into the file, and then I call the WriteLine method to write the contents of the TextBox into the file. To read the information out, I ask the user permission by opening the file dialog. If they click OK and I have selected a file, then I open the file via a stream, create a reader to help me see the contents of the file, and then I call the reader.ReadToEnd method, and then I show the results in the MessageBox.
I can use this technique for images too. Down here, I am opening a file dialog, using the PNG or JPEG extensions. And the critical parts here are opening the stream, which will be, let's say a PNG file, I will create a brand new bitmap image. I'll get the source with the stream information here, I will set the source from the stream on the bitmap image, and then once I have the bitmap image, I can assign it as a source to any valid image control. Let's show this in action.
Click to save on the local system. I'll type a new note here, "This is my note," and click on Save. It prompts me to save it. I'm going to call this one demonote, and then I will click on Save. Next, to open it, I will click Open Notes. I will choose demonote and there you can see my text. For the last example, I will click on the Open Image from Local. I'm in my Documents folder. Here I have a png file called grapes.png. Click on Open and there it is.
In summary, I would like to repeat something I said earlier in the movie. You can store in the local file system as long as you get your users' permissions, where you are running in elevated trust. If you don't have this permission, you can store data only in isolated storage which offers a small but safe location for your data.
Find answers to the most frequently asked questions about Silverlight 5 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.
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.