Monday, February 6, 2012

Windows Phone - Application Architecture (6)

Hello

Basic Navigation
Navigation means moving from page to page, somewhat like in web browser when you navigate back and forward. This means more then one page on the application.

Sample code

BasicNavigation_NK.zip

The sample use two pages : regular MainPage and SecondPage. It navigate from one page to the other. However , navigation is done differently.

Change "page name" to "Main Page" in MainPage.xaml.

Add new page : Mark the project name , right click the mouse , Add new item , choose "Windows Phone Portrait Page"  name it SecondPage.xaml and change in SecondPage.xaml "page name" to "Second Page".

MainPage
The button control the navigation to the second page using NavigateService.Navigate and specifying the target URI- /SecondPage.xaml in this case. Click on the button will load new object type SecondPage.

   




SecondPage

The button control the navigation using NavigateService.GoBack. Click on the button will load the previous page i.e. MainPage using the same object.


      





Run the application, push Forward and Back and notice that "Page Object Identifier" is the same for MainPage and different every time you access SecondPage - why ?

Data Access in Pages

Source sample
Store data in App object. Access via Application.Current
PagesDataAccess_NK.zip


In App.xaml.cs define property str

Access the property from some page e.g  MainPage.xaml.cs



Store data in PhoneApplicationService.Current.State
Set time into the State in SecondPage.xaml.cs using key "SecondPageAccessTime"



Get time from MainPag.xaml.cs using the  using the same key "SecondPageAccessTime"


Note that State persist deactivate\reactivate (see next Tomsstoning)


Multitasking


Tombstoning - Windows Phone 7.0
Suppose you invoke application A , then you push the phone Start button and invoke application B, then you push the phone button Start and invoke application C. Then you push the phone Back button - you arrives back to application B, another click on Back and you are back to application A.


What is going on here ?
WP put the application on a stack when you click the Start button and pop application from the stack when you click the back button. BTW, the same concept is used for pages.


Note that invoking B after A put A into the stack and terminate (deactivate) it. Going back from B to A pop A from the stack and re- execute  (reactivate) it.


Every process on the stack is terminated  (deactivated), it's state is tombstoned.






Tombstoning persistence sample
TombstoningPersistence_NK.zip


Following sample uses PhoneApplicationService.State as presistence mechanizm cross application deactivation\activation. The sample allows to toggle the background color between red and green via button. The color  is saved in PhoneApplicationService after the the user hit the Start button and reused after the user hit the Back button and reach the application.


The sample use two virtual methods of Page class :






Note that you can force Tombstoned state while debugging. It can be done via the project properties->Debug



Change application setting so that "Tombstone upon deactivation while debugging " is checked
Run the application, click "Toggle Backgound Color" button , click Start button, click back - the application has preserved the last background color.

Now remark the state preservtion in OnNavigateTo and repeat - now the background color is NOT preseved because the state is Tombstone so state was not preserved by Windows Phone  and it was not used when navigated to the page.

Dormancy - Windows Phone 7.1

Windows Phone 7.1 support a new state called Dormant.
The difference between Dormant and Tombstoned is :

  • Dormant means that the application is on memroy while Tombstoned means the application is not in memory
  • Dormant means that the application state is saved while Tombstoned means the state is not saved




Going from Dormant to Running is fast and is called - Fast Application Switch (FAS)


Source sample
TombstoningPersistence_NK.zip

Make sure in application setting that "Tombstone upon deactivation while debugging " is NOT checked.
Now remark the state preservtion in OnNavigateTo


Run the application, click "Toggle Backgound Color" button , click Start button, click back - the application has preserved the last background color due to Dormancy i.e. state (background color) was preserved by Windows Phone !

A word of cautious : even though Dormant exist in Windows Phone the application should save the state anyway. That's because the memory is limited and not all application deactivated can reside in memory and they will be Tombstoned. However, you need to restore state only when the application is back from Tomstoned NOT when it's back from Dormant because Windows phone does it for you. To destinguish between these two situation use IsApplicationInstancePreserved via the Activated handler


Isolated storage
Isolated storage is a per application dedicated disk space. An application has access only to its isolated storage it can not access the system or other applications storage.
Isolated storage handles three data types :

  • Setting file
  • File\directory
  • Data base


Isolated storage file sample

IsolateStorage_NK.zip


This sample demonstrate the use of file operations : read\write\delete - each has a separated button. The user can type a text which is appended to a file upon click on "Write File" button.
The user can read the file content upon click on "Read File" button.
The user can delete the file using "Delete File" button.



MainPage.xaml




Most important classes here are :


MainPage.xaml.cs






Nathan

No comments:

Post a Comment