Isolated Storage in SL2 provides users with a virtual file system that stores a small amount of data information like cookies. By default, this Storage space is only 1 MB, you can also make adjustments. In actual use, for example, if you need to enter a multi-page form, you can save the previously filled content to an independent Bucket During page switching to avoid loss. This article provides exercises for this case:
First, on the first page, set a Textbox for users to enter information:
<Grid x:Name="LayoutRoot" Background="White" Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> </Grid.RowDefinitions> <TextBox x:Name="UserName" Background="AntiqueWhite" Grid.Row="0" BorderThickness="3" Margin="5,2,5,0"></TextBox> <TextBlock x:Name="lblMessage" Grid.Row="1" Text="Enter your name"></TextBlock> <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="5,5,5,5"> <Button Content="Write" Margin="5,0,5,0" Click="Button_Write_Click"></Button> <Button Content="Read" Margin="5,0,5,0" Click="Button_Read_Click"></Button> <Button Content="Next Page" Margin="5,0,5,0" Click="Button_NextPage_Click"></Button> </StackPanel></Grid>
The three buttons have the following functions: using System. IO. IsolatedStorage and using System. IO must be used:
Using System; using System. windows; using System. windows. controls; using System. IO. isolatedStorage; using System. IO; namespace IsolatedStorage {public partial class Page: UserControl {public Page () {InitializeComponent () ;}// write data to the bucket public void Write_Data () {// create a bucket IsolatedStorageFile store = IsolatedStorageFile. getUserStoreForApplication (); // create a data stream IsolatedStorageFileStream = store. createFile ("data.txt"); // write data
StreamWriter writer = new StreamWriter (stream );
Writer. write (UserName. text. toString (); writer. close () ;}// read public string Read_Data () {string returnValue; IsolatedStorageFile store = IsolatedStorageFile. getUserStoreForApplication (); if (store. fileExists ("data.txt") {// create a data stream IsolatedStorageFileStream = store. openFile ("data.txt", FileMode. open); // read data StreamReader reader = new StreamReader (stream); // value to returnValue = reader. readLine (); reader. close () ;} else {returnValue = null;} // return returnValue;} // click the Write button to Write data private void Button_Write_Click (object sender, RoutedEventArgs e) {Write_Data (); lblMessage. text = "Data written to data.txt";} // click the Read button to Read the private void Button_Read_Click (object sender, RoutedEventArgs e) {lblMessage. text = "Your name is:" + Read_Data () ;}// click the NextPage button to switch to the private void Button_NextPage_Click (object sender, RoutedEventArgs e) page) {// write data to the bucket Write_Data (); App. navigate (new Page2 ());}}}
Write the name in Page1 and click NextPage. First, let's look at Page1 (it seems that cnblogs does not support independent storage programs). If you are wronged, you can only look at the figure:
Page2 is used to display the data stored in Page1. XAML Code:
<Grid x:Name="LayoutRoot" Background="White"> <StackPanel Orientation="Vertical"> <TextBlock Text="This is Page2." Margin="0,0,0,5"></TextBlock> <TextBlock x:Name="lblMessage" Margin="0,0,0,5"></TextBlock> <Button Content="Back" Width="40" Click="Button_Click"></Button> </StackPanel></Grid>
using System;using System.Windows;using System.Windows.Controls;using System.IO.IsolatedStorage;using System.IO;namespace IsolatedStorage{ public partial class Page2 : UserControl { public Page2() { InitializeComponent(); string DataValue = Read_Data(); if (DataValue != null) { lblMessage.Text = "Written in Page1 is: " + DataValue; } else { lblMessage.Text = "Nothing written in Page1"; } } public string Read_Data() { string returnValue; IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); if (store.FileExists("data.txt")) { IsolatedStorageFileStream stream = store.OpenFile("data.txt", FileMode.Open); StreamReader reader = new StreamReader(stream); returnValue = reader.ReadLine(); reader.Close(); } else { returnValue = ""; } return returnValue; } private void Button_Click(object sender, RoutedEventArgs e) { App.Navigate(new Page()); } }}
The data stored in Page1 has been obtained in Page2 ,:
For this example, see CHAPTER 15 isolated storage in Pro Silverlight 2 in C #2008.
Source code download