Starting Windows Phone 8 Development: Lesson 1. Application Layout and Event HandlerThe beginning of Windows Phone 8 Development: lesson 2. Access to the local storage applicationThe beginning of Windows Phone 8 Development: lesson 3. Navigating through pages and passing parametersThe beginning of Windows Phone 8 Development: lesson 4. Communication with services and data bindingAnd so, let's proceed to the second lesson. I hope it will be more interesting for you. Many developers for smartphones are interested in the availability of storage on the device for their applications, as well as its size and use. Each application has its own isolated local storage. It is independent of other applications and the OS, and only this application has access to it.
Two questions immediately arise: the ability to use an SD card and the size of the available space. The answer to the first question is yes, you can use an SD card. The answer to the second question is unlimited. That is all free space.
')
As for the application that we will create in this lesson, it will use the local storage for reading / writing text messages and a counter. The counter will increase each time you start the application, as well as the last message you entered will be displayed.
Appearance

As you have already noticed, two TextBlocks are used to display messages and a counter, and one TextBox to enter a new message. The layout as always uses XAML and my favorite StackPanel:

Code <StackPanel Grid.Row="1" Margin="12,0,12,0"> <TextBlock x:Nane="CounterBlock" HorizontalAlignment="Left" Padding="0,0,0,10" TextWrappings"Wrap" Text="You have accessed this page ## times." VerticalAlignment="Top"/> <TextBlock x:Nane="LastMessageBlock" HorizontalAlignment="Left" Padding="0,0,0,30" TextWrapping="Wrap" Text="Your last message was: ##." VerticalAlignment="Top"/> <TextBox Height="72" x:Name="NewMessage" TextWrapping="Wrap"/> <Button content="save New Message" x:Name="SaveNewMessage" /> </StackPanel>
This time for TextBlocks we set the Name property, since we will transfer the content to them from the outside. Now consider what happens when the page loads.
Loading page
Before adding logic, make sure the necessary namespaces are added:

Code using System.I0.IsolatedStorage; using System.I0; namespace Lesson2 { ....
When you load the page in the first place you need to make sure that the file in which the counter is stored and the message exists. If it exists, then we read the necessary data and transfer it to the global variable. If there is no file, then we write in the counter β0β, and in the messages βEmptyβ (No messages ..!) Of the new file. I will use a file called LS.txt (short for Local Storage).
Well, it is natural to pass the values ββin TextBlocks by replacing the β##β characters in the string with the values ββof variables using the string.Replace function.

Code private int counter; private string message; private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e) { using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (appStorage.FileExists("LS.txt")) { using (var file = appStorage.OpenFile("LS.txt", FileMode.Open)) { using (var reader = new StreamReader(file)) { counter = int.Parse(reader.ReadLine()); message = reader.ReadLine(); } } } else { using (var file = appStorage.OpenFile("LS.txt", FileMode.Create)) { using (var writer = new StreamWriter(file)) { writer.WriteLine("0"); writer.WriteLine("No messages..!"); } } } } CounterBlock.Text=CounterBlock.Text.Replace("##",counter.ToString()); LastMessageBlock.Text = LastMessageBlock.Text.Replace("##", message); }
Actions on closing the application
If the application closes when you click the Back or Start buttons, we replace the LS.txt file with a new one, after updating the data with the actual ones. At first we increase the counter, and then we write down the message.

Code protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedFrom(e); using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { using (var file = appStorage.OpenFile("LS.txt", FileMode.Create)) { using (var writer = new StreamWriter(file)) { writer.WriteLine((counter+1).ToString(); writer.WriteLine(message); } } } }
Well, at the end of everything you need to save the new message and exit the application by calling the OnNavigatedFrom event.
Saving a new message
We transfer the contents of the TextBox to a variable, and display a text message to the user about successful saving.

Code private void SaveNewMessage_Click(object sender, RoutedEventArgs e) { message = NewMessage.Text; MessageBox.Show("Changes saved successfully"); }
We are testing
Run by clicking on F5 and waiting for the download. After loading see the counter shows 0, and there are no messages. We collect new and save.

After clicking on Save, we see a text message. After a successful save, the increment of the counter is called, and the application closes itself.

Now run it again. Everything is working. Everything is updated.

