• No results found

Using Isolated Storage

In document Microsoft Silverlight 4 Step by Step (Page 188-194)

Isolated storage is so called because the data is isolated in an obscure set of directories and files on your hard drive . Silverlight assigns isolated storage space on a per-domain basis, re- serving a default limit of 1 MB per domain . In other words, if you have several applications using the same domain, the total default limit for all of them is 1 MB . However, for each out of browser application, the default limit is raised to 25 MB . You can increase these limits, but it will require user approval to do so . In this section, you’ll learn how to read from and write to isolated storage .

1 . Create a new Silverlight application and name it SbSCh8_2 .

2 . Add a TextBox, a TextBlock, and two Button controls to the Mainpage .xaml file .

3 . Name the first Button button1 and change its Content property value to Write . Name

the second Button button2 and change its Content property value to Read .

5 . Add the following code to the top of MainPage .xaml .cs, beneath the existing using

statements:

using System.IO;

using System.IO.IsolatedStorage;

6 . The Write button has an event handler named button1_Click . You want this event han-

dler to take the text the user types into the TextBox and write it to isolated storage . Start by setting up an isolated storage file called store .

using (IsolatedStorageFile store =

IsolatedStorageFile.GetUserStoreForApplication()) {

}

7 . Now that you have a reference to an isolated storage file, create a new file in it using

the OpenFile method . Here’s how you can open a new file called MyStore .Text . Note This OpenFile call will create the file if it doesn’t already exist .

store.OpenFile("MyStore.Text", FileMode.OpenOrCreate, FileAccess.Write)

8 . You can pass this entire line of code to a StreamWriter to make it easy to write to the

file .

using(StreamWriter sw = new StreamWriter(

store.OpenFile("MyStore.Text", FileMode.OpenOrCreate, FileAccess.Write))) {

}

9 . Now that you have a StreamWriter object called sw, it’s easy to write the content of

textBox1 to it .

10 . Here’s the full button1_Click event handler:

private void button1_Click(object sender, RoutedEventArgs e) {

using (IsolatedStorageFile store =

IsolatedStorageFile.GetUserStoreForApplication()) {

using(StreamWriter sw = new StreamWriter(

store.OpenFile("MyStore.Text", FileMode.OpenOrCreate, FileAccess Write))) {

sw.WriteLine(textBox1.Text); }

} }

11 . Now you can implement the code for button2 . In this event handler, you want to read

back whatever was written into isolated storage and write it into the TextBlock . The code for this click handler is very similar to what you just wrote, except this time the ap- plication is reading .

private void button2_Click(object sender, RoutedEventArgs e) {

using (IsolatedStorageFile store =

IsolatedStorageFile.GetUserStoreForApplication()) {

using(StreamReader sr = new StreamReader(

store.OpenFile("MyStore.Text", FileMode.Open, FileAccess.Read))) {

textBlock1.Text = sr.ReadToEnd(); }

} }

13 . Type something into the TextBox and press the Write button and the application will

write the text to isolated storage .

14 . Press the Read button and the application will read the text from isolated storage and

display it in the TextBlock .

15 . Take note of the address from which your application is running (check your browser’s

address field) . For example, the following screen shows that the application is running from http://localhost:10216 . You will likely have a similar address, but the port number (10216 in this case) might be different . That’s fine—just remember which port number your application is using .

16 . Right-click anywhere in your application and a pop-up menu will appear . It likely

just contains the item “Silverlight .” Select that, and you’ll see the Microsoft Silverlight Configuration dialog box . Select the Application Storage tab and you’ll see your Web site listed .

Note that this dialog box contains a check box that allows users to enable or disable isolated storage (the dialog box calls it Application Storage) . Be sure to wrap plenty of error checking around any code you write that reads or writes to isolated storage, because there’s every chance the user could have disabled it . You can detect whether isolated storage is enabled by using the IsolatedStorageFile.IsEnabled property .

Increasing Isolated Storage

As mentioned earlier, a domain has a maximum of 1 MB for isolated storage with an in- browser application and 25 MB for an out of browser application . The space available for isolated storage, however, can be increased . But the user has to allow it .

Increase the amount of isolated storage

1 . Stop the application, and go to the button1_Click event handler . Find the line that reads:

using(StreamWriter sw = new StreamWriter(

Add the following code above that line:

Int64 newSpace = 2097152;

Int64 curSpace = store.AvailableFreeSpace; if (curSpace < newSpace)

{

if (!store.IncreaseQuotaTo(newSpace)) {

MessageBox.Show("Sorry, the user refused!"); }

else {

MessageBox.Show("Your Isolated Storage is now 2Mb"); }

}

This code checks the available free space in isolated storage . When the free space is less than newSpace (2097152 bytes or 2 MB), the code calls the IncreaseQuotaTo() method . In this case, it tries to increase the quota to 2 MB . The attempt causes Silverlight to raise a dialog box that asks the end user for permission to increase isolated storage space . If the user answers yes, Silverlight increases the quota and the code displays a

MessageBox that reflects the new size . If the user answers no, the quota stays the same .

2 . Press F5 to execute the application . This time, when you click the Write button, you’ll

see a dialog box that asks if you want to increase the size of available storage .

3 . Click No, and you’ll see the “Sorry the user refused!” message box, and you’ll be re-

turned to your application .

4 . Press the Write button again and this time click Yes . Now you’ll see the message that

says your isolated storage is now 2 MB and you’ll be returned to the application .

5 . Right-click the application and open the Silverlight configuration dialog box . Take a

This describes just about everything you need to know about isolated storage and how to use it in your applications . You can now take advantage of this incredibly useful tech- nology to provide productive experiences both online and offline .

In document Microsoft Silverlight 4 Step by Step (Page 188-194)