Windows Phone development path (18) independent storage

Source: Internet
Author: User

As mentioned above, you must use independent storage to store data during multiple running times of a program. Any program installed in Windows Phone 7 can access its permanent disk storage area, which is called independent storage. In the following project, the total number of clicks is used as temporary data, that is, as part of the page status. The background color is shared by all instances as the application settings.

  Mainpage. XAML code:

<Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
<Grid. rowdefinitions>
<Rowdefinition Height = "Auto"/>
</Grid. rowdefinitions>

<Grid. columndefinitions>
<Columndefinition width = "Auto"/>
<Columndefinition width = "*"/>
</Grid. columndefinitions>

<Textblock text = "the number of times the screen is clicked :"
Grid. Row = "0"
Grid. Column = "0"/>

<Textblock text = ""
Name = "txtnum"
Grid. Row = "0"
Grid. Column = "1"/>

</GRID>

  Mainpage. xaml c # code:

Namespace silverlightisolatedstorage
{
Public partial class mainpage: phoneapplicationpage
{
// Declare global variables
Random Rand = new random ();
Int numtaps = 0;
Phoneapplicationservice appservice = phoneapplicationservice. Current; // gets the phoneapplicationservice object of the current application, used to save temporary data

// Constructor
Public mainpage ()
{
Initializecomponent ();

Updatenumtaps (numtaps); // The number of initialization clicks on the screen
Brush = (application. Current as APP). backgroundbrush; // set access to the app class for independent storage

If (brush! = NULL)
{
This. contentpanel. Background = brush; // obtain the backgroundbrush attribute from the app class.
}
}

Protected void updatenumtaps (INT num) // you can specify the number of update clicks.
{
Txtnum. Text = string. Format ("{0} Times", num );
}

Protected override void onmanipulationstarted (manipulationstartedeventargs e) // rewrite the virtual method of the base class control (inherited from mainpage) (called when the manipulationstarted event occurs)
{
// Todo: When you click the screen, the background color is randomly changed and the color is saved to the public attributes of the app class. Then, the updatenumtaps () method is called to update the number of clicks.
Solidcolorbrush brush = new solidcolorbrush (color. fromargb (255, (byte) Rand. next (256), (byte) Rand. next (256), (byte) Rand. next (256 )));

This. contentpanel. Background = brush;

(Application. Current as APP). backgroundbrush = brush; // save data to the app class for independent storage

Updatenumtaps (++ numtaps); // call Method

E. Complete ();
Base. onmanipulationstarted (E); // call the base class virtual Method
}

// Although most of the tasks of the program's tombstone restoration operations are left to the program itself for processing, by rewriting the onnavigatedfrom and onnavigatedfrom methods, the program can be reactivated and loaded to the correct page.
Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs e) // rewrite the virtual method of the base-class page (called when the page leaves)
{
// Todo: Save the temporary data to the State Dictionary of phoneapplicationservice.
Appservice. State ["numtaps"] = numtaps;

Base. onnavigatedfrom (E );
}

Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs e) // rewrite the virtual method of the base-class page (called when the page becomes an active page of the Framework)
{
// Todo: Load numtaps
If (appservice. state. containskey ("numtaps") // determines whether the key is included.
{
Numtaps = (INT) appservice. State ["numtaps"];
Updatenumtaps (numtaps );
}

Base. onnavigatedto (E );
}
}
}

Effect


Before the program exits, the program exits and restarts.

  Conclusion: 1. If you want to save temporary data (data between one execution cycle), you 'd better use the phoneapplicationservice class to save the data. 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.
2. Is there a very confusing question? If the onnavigatedfrom and onnavigatedto methods are not rewritten, the program can still correctly display the number of times the screen was last clicked when the tombstone was restored, because in Windows Phone 7, most of the tasks of the program's tombstone recovery operations are handed over to the program itself for processing. But it is best to rewrite these two methods, because this ensures that the correct page can be loaded when the program is re-activated.

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.