Windows Phone development (8): independent storage isolated storage

Source: Internet
Author: User

Foreground XAML

<Phone: phoneapplicationpage X: class = "isolatedstorageapp. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Phone =" CLR-namespace: Microsoft. phone. controls; Assembly = Microsoft. phone "xmlns: shell =" CLR-namespace: Microsoft. phone. shell; Assembly = Microsoft. phone "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008" Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D" D: designwidth = "480" D: designheight = "768" fontfamily = "{staticresource quota}" fontsize = "{staticresource quota}" foreground = "{staticresource quota}" supportedorientations = "portrait" orientation = "portrait" Shell: systemtray. isvisible = "true"> <! -- Layoutroot is the root grid where all page content is placed --> <grid X: Name = "layoutroot" background = "Transparent"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <! -- Titlepanel contains the name of the application and page title --> <stackpanel X: Name = "titlepanel" grid. row = "0" margin = ","> <textblock X: name = "applicationtitle" text = "isolated storage" style = "{staticresource phonetextnormalstyle}"/> <textblock X: name = "pagetitle" text = "standalone storage" margin = "9,-7,0, 0" style = "{staticresource phonetexttitle1style}"/> </stackpanel> <! -- Contentpanel-place additional content here --> <grid X: Name = "contentpanel" grid. row = "1" margin = ","> <textbox Height = "72" horizontalalignment = "Left" margin =, 416 "name =" txtwrite "text =" "verticalalignment =" TOP "width =" "/> <button content =" save "Height =" 72 "horizontalalignment =" left" margin = "-1,160, 160 "name =" btnwrite "verticalignment =" TOP "width =" "Click =" txtwrite_click "/> <t Extblock Height = "149" horizontalalignment = "Left" margin = "12,275, 403 "name =" txtread "text =" "verticalalignment =" TOP "width =" "/> <button content =" read "Height =" 72 "horizontalalignment =" left" margin = "-1,450, 160 "name =" btnread "verticalignment =" TOP "width =" "Click =" btnread_click "/> </GRID> <! -- Sample Code showing usage of ApplicationBar --> <! -- <Phone: phoneapplicationpage. applicationBar> <shell: ApplicationBar isvisible = "true" ismenuenabled = "true"> <shell: applicationbariconbutton iconuri = "/images/appbar_button1.png" text = "button 1"/> <shell: applicationbariconbutton iconuri = "/images/appbar_button2.png" text = "button 2"/> <shell: ApplicationBar. menuitems> <shell: applicationbarmenuitem text = "menuitem 1"/> <shell: applicationbarmenuitem text = "menuitem 2"/> </shell: ApplicationBar. menuitems> </shell: ApplicationBar> </Phone: phoneapplicationpage. applicationBar> --> </Phone: phoneapplicationpage>

 

 

Backend Cs:

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;using System.IO;using System.IO.IsolatedStorage;namespace IsolatedStorageApp{    public partial class MainPage : PhoneApplicationPage    {        // Constructor        public MainPage()        {            InitializeComponent();        }        private void txtWrite_Click(object sender, RoutedEventArgs e)        {            //Obtain the virtual store for application            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();            //Create a new folder and call it "FavorFolder"            myStore.CreateDirectory("FavorFolder");            //Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)            //Also take the text contents from the txtWrite control and write it to myFile.txt            StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("FavorFolder\\myFile.txt", FileMode.OpenOrCreate, myStore));            writeFile.WriteLine(txtWrite.Text);            writeFile.Close();        }        private void btnRead_Click(object sender, RoutedEventArgs e)        {            //Obtain a virtual store for application            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();            //This code will open and read the contents of myFile.txt            //Add exception in case the user attempts to click Read button first.            StreamReader readFile = null;            try            {                readFile = new StreamReader(new IsolatedStorageFileStream("FavorFolder\\myFile.txt", FileMode.Open, myStore));                string fileText = readFile.ReadLine();                //The control txtRead will display the text entered in the file                txtRead.Text = fileText;                readFile.Close();            }            catch            {                txtRead.Text = "Need to create directory and the file first.";            }        }    }}

Summary: If you want to save temporary data (data between one execution cycle), you 'd better use the phoneapplicationservice class to save it; if you want to save application settings (save data between multiple execution cycles) it is best to use the independent storage isolatedstoragesettings class to save.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.