Creating Windows Phone Applications<br />
WinodwsPhoneicons and splashscreens<br />
Topics<br />The Windows Phone Start screen and Application List<br />Icons in Sliverlight and XNA<br />Customising Icons<b...
The Windows Phone UI<br />The Windows Phone user interface is quite different from any other Microsoft Windows interface<b...
The Start Screen<br />The default start screen contains links to built in Windows Phone features<br />Users can add their ...
The Application List<br />This is a list of all the applications on the phone<br />Each application is represented by its ...
Icons in a Silverlight program<br />When you create a Silverlight application Visual Studio creates default versions of th...
The ApplicationIcon.png image<br />The ApplicationIcon.png file holds the icon for the application in the Application List...
The Background.png image<br />The Background.png file holds the icon for the application in the Start Screen<br />9<br />
Icons in an XNA program<br />The icons for an XNA program are different<br />The file names are different and there is als...
The GameThumbnail.png image<br />The GameThumbnail.png file holds the application icon for an XNA game<br />11<br />
The Background.png image<br />The Background.png file holds the Start screen image for an XNA game<br />12<br />
Customising Icons<br />It is important to create good looking icons for your programs<br />These icons will also be used t...
Splash Screens<br />A splash screen is an image that is displayed as a program starts running<br />They are often used to ...
Silverlight Splash Screens<br />Silverlight applications are provided with a default splash screen image which just shows ...
Splash screens in XNA<br />XNA projects do not have a splash screen by default<br />An XNA game is expected to load its co...
XNA Loading Strategies<br />If you make a game with a large amount of content you should not load it all in the LoadConten...
Summary<br />Applications and games on Windows Phone are given icons to identify them<br />There are images for the applic...
Persisting data in isolatedstorage<br />
Topics<br />Windows Phone applications and isolated storage<br />Using isolated storage to store files<br />Storing name/v...
Isolated Storage<br />This storage is called isolated because each application on the phone has its own area<br />One appl...
Jotpad program<br />This is the first version of the jotpad program<br />It displays a textbox for the user to enter text<...
The Save button behaviour<br />privatevoidsaveButton_Click(object sender, RoutedEventArgs e){saveText("jot.txt", jotTextBo...
The saveText method<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageF...
Using IsolatedStorage<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorag...
Creating a file<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageFile....
Writing to the file<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageF...
The Load button behaviour<br />privatevoidloadButton_Click(object sender, RoutedEventArgs e){string text;if ( loadText("jo...
Loading from Isolated storage<br />try{using (IsolatedStorageFileStreamrawStream =isf.OpenFile(filename, System.IO.FileMod...
The Isolated Storage File Store<br />Your applications can create many files in isolated storage<br />They can also build ...
Demo 1: Jotpad Demo<br />Demo<br />31<br />
Using Settings Isolated storage<br />Creating files in isolated storage is useful, but often a program only wants to store...
Settings and Dictionaries<br />The settings storage works like a Dictionary collection<br />A Dictionary holds a collectio...
A Dictionary example<br />classPerson{publicstring Name;publicstring Address;publicstring Phone;}<br />The Person class ho...
A Dictionary example<br />Dictionary<string, Person> Personnel = newDictionary<string, Person>();<br />This creates a Dict...
Storing in the Dictionary<br />Person p1 = newPerson { Name = "Rob",                          Address = "His House",      ...
Reading from a Dictionary<br />PersonfindPerson = Personnel["Rob"];<br />We can use a string indexer to locate items in th...
Checking for Items<br />if (Personnel.ContainsKey("Jim")){// If we get here the dictionary     // contains Jim}<br />A Dic...
Dictionaries in action<br />Dictionaries are very useful for storing collections that you want to index on a particular ke...
Dictionaries and Isolated Storage<br />The IsolatedStorageSettings class provides a Dictionary based model for storing and...
Saving using settings<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =Isolated...
Getting the isolated store<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =Iso...
Removing an old version<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =Isolat...
Saving the data<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =IsolatedStorag...
Saving items<br />You can save objects other than strings<br />Each object must have a unique name<br />Your program must ...
Reading from settings storage<br />Reading is the reverse of writing<br />Your program must provide the key of the item it...
Loading from the setting storage<br />privateboolloadText(string filename, outstring result) {IsolatedStorageSettingsisola...
Managing the loading <br />result = "";try {result = (string) isolatedStore[filename];}catch {returnfalse;}<br />Because i...
Isolated Storage<br />A program can create and use as many files as the application requires<br />It is also possible to c...
Demo 2: Settings Jotpad<br />Demo<br />50<br />
Summary<br />Windows Phone provides “local” storage for applications<br />Data stored is local to an application and not s...
Persistingapplicationstate<br />
Topics<br />The Windows Phone process model<br />Understanding “tombstoning”<br />Tombstone events in applications<br />Ma...
Windows Phone Task Management<br />The Windows Phone platform uses a multi-tasking operating system<br />This allows phone...
Single Tasking Applications<br />In the present version of Windows Phone it is not possible to run more than one applicati...
Why Single Task?<br />Removing multi-tasking stops one application from being affected by another as it runs<br />Backgrou...
The Back and Start buttons<br />The Back button can be used to exit from one program and return to one being used earlier<...
Application Switching<br />This means that an application must be adept at stopping and starting<br />The aim is to presen...
Tombstoning<br />Tombstoning is the name given to the act of stopping an application so that a user can start another<br /...
Tombstones and code design<br />The way that a program responds to tombstone events has an impact on the user experience<b...
Jotpad and tombstones<br />We are going to make the Jotpad program “tombstone friendly”<br />The program will automaticall...
“Tombstone” events<br />There are four “tombstone” events<br />Launching – a new copy of the program has been started by t...
Tombstone event methods<br />Each of the events is mapped onto a method in the App.xaml.cs file<br />By adding code into t...
Tombstone event methods<br />privatevoidApplication_Launching(object sender, LaunchingEventArgse){}privatevoidApplication_...
Saving on closing<br />privatevoidApplication_Closing(object sender, ClosingEventArgse){MainPagejotPadPage = (MainPage)Roo...
MainPage Save method<br />publicvoid Save(){saveText("jot.txt", jotTextBox.Text);}<br />This is the Save method in the Mai...
Saving to memory<br />The save methods that we have seen up to now use persistent storage to hold status<br />This can be ...
Saving on tombstoning<br />privatevoidApplication_Deactivated(object sender, DeactivatedEventArgse){MainPagejotPadPage = (...
Saving on tombstoning<br />privatevoidApplication_Deactivated(object sender, DeactivatedEventArgse){MainPagejotPadPage = (...
The SaveStateText method<br />privatevoidSaveStateText (string filename, string text){IDictionary<string, object> stateSto...
The loadStateText method<br />privateboolloadStateText(string filename, outstring result)<br />{<br />IDictionary<string, ...
loadTextvsloadStateText<br />The loadStateText method is very similar to the loadState method<br />They are both used in e...
Reloading data when restarted<br />A program can get Launching or Activated messages when it restarts<br />Launching means...
Loading data on restart<br />The JotPad program tries to load from the state memory first and then loads from isolated sto...
The Load method<br />publicvoid Load()<br />{<br />string text;<br />if (loadStateText("jot.txt", out text))  {<br />jotTe...
Loading at program start<br />privatevoidPhoneApplicationPage_Loaded(object sender, RoutedEventArgse)<br />{<br />    Load...
Using Visual Studio<br />Visual Studio retains contact with an application when it is tombstoned<br />If you press the Sta...
Demo 1: Jotpad Demo<br />Demo<br />78<br />
Summary<br />The Windows Phone operating system uses a single tasking model for applications<br />Start and Back buttons a...
Launchers and choosers<br />
Topics<br />Launchers and choosers in context<br />Tombstoning and Launchers and Choosers<br />Using a Launcher<br />Start...
Launchers and Choosers<br />Windows Phone provides a way for programs to interact with the phone itself:<br />Take photogr...
User Involvement<br />Note that in all the applications the user is directly involved and has the final say on the action<...
Launchers vs Choosers<br />Applications call a  Launcher makes use of a phone feature<br />Place a phone call, send an ema...
Calling a Launcher or Chooser<br />When an application calls a Launcher or Chooser the new task gets control<br />When the...
Launchers<br />These are the Launchers available:<br />PhoneCallTask<br />EmailComposeTask<br />SmsComposeTask<br />Search...
Using a Launcher<br />As an example, we could add an email feature to the JotPad application<br />This would allow the use...
The Mail button<br />privatevoidmailButton_Click(object sender, RoutedEventArgse){sendMail("From JotPad", jotTextBox.Text)...
The Mail button<br />privatevoidsendMail(string subject, string body){EmailComposeTask email = newEmailComposeTask();email...
The Tasks namespace<br />usingMicrosoft.Phone.Tasks;<br />In order to use the Launcher and Chooser classes by name an appl...
Demo 1: Email Jotpad<br />Demo<br />91<br />
Choosers<br />These are the Choosers available:<br />CameraCaptureTask<br />EmailAddressChooserTask<br />PhoneNumberChoose...
Choosers<br />Before an application calls a chooser it can bind to an event that the chooser task generates<br />This is u...
Picture display application<br />The picture display application uses the PhotoChooserTask to allow the user to select a p...
Creating the PhotoChooserTask<br />PhotoChooserTaskphotoChooser;<br />publicMainPage()<br />{<br />InitializeComponent();<...
The Completed event handler<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == ...
The TaskResult field<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskRes...
The OriginalFileName field<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == T...
Create a new image<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskResul...
Load button event handler<br />privatevoidloadButton_Click(object sender, RoutedEventArgse){photoChooser.Show();}<br />Whe...
Demo 2: Picture display<br />Demo<br />101<br />
Summary<br />Launchers and Choosers provide a way that applications can use phone features<br />Launchers just start a pho...
Upcoming SlideShare
Loading in …5
×

WP7 HUB_Creando aplicaciones de Windows Phone

886 views

Published on

Fase 3.6

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
886
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

WP7 HUB_Creando aplicaciones de Windows Phone

  1. 1. Creating Windows Phone Applications<br />
  2. 2. WinodwsPhoneicons and splashscreens<br />
  3. 3. Topics<br />The Windows Phone Start screen and Application List<br />Icons in Sliverlight and XNA<br />Customising Icons<br />Splash Screens <br />
  4. 4. The Windows Phone UI<br />The Windows Phone user interface is quite different from any other Microsoft Windows interface<br />Users can “pin” frequently used applications to the Start screen<br />A user can always navigate to the Start screen by pressing the Start button on the phone<br />From the Start screen a user can move to the Application list<br />4<br />
  5. 5. The Start Screen<br />The default start screen contains links to built in Windows Phone features<br />Users can add their favourite contacts, media and applications to the screen <br />Each application is represented by a tile<br />5<br />
  6. 6. The Application List<br />This is a list of all the applications on the phone<br />Each application is represented by its icon and the title of the application<br />6<br />
  7. 7. Icons in a Silverlight program<br />When you create a Silverlight application Visual Studio creates default versions of the icon files<br />We can edit the files to make custom items<br />7<br />
  8. 8. The ApplicationIcon.png image<br />The ApplicationIcon.png file holds the icon for the application in the Application List<br />8<br />
  9. 9. The Background.png image<br />The Background.png file holds the icon for the application in the Start Screen<br />9<br />
  10. 10. Icons in an XNA program<br />The icons for an XNA program are different<br />The file names are different and there is also an icon for use if a game is deployed to an Xbox 360<br />10<br />
  11. 11. The GameThumbnail.png image<br />The GameThumbnail.png file holds the application icon for an XNA game<br />11<br />
  12. 12. The Background.png image<br />The Background.png file holds the Start screen image for an XNA game<br />12<br />
  13. 13. Customising Icons<br />It is important to create good looking icons for your programs<br />These icons will also be used to brand your program in the Windows Phone Marketplace<br />This is probably something worth employing an artist for…<br />13<br />
  14. 14. Splash Screens<br />A splash screen is an image that is displayed as a program starts running<br />They are often used to brand an application and give a user something to watch as a program loads itself into memory<br />14<br />
  15. 15. Silverlight Splash Screens<br />Silverlight applications are provided with a default splash screen image which just shows a clock<br />You can replace this with a jpeg image of your own<br />The file is called SplashScreenImage.jpg<br />15<br />
  16. 16. Splash screens in XNA<br />XNA projects do not have a splash screen by default<br />An XNA game is expected to load its content and then start drawing the screen<br />The Windows Phone operating system expects an application to start drawing on the screen within 5 seconds of starting running<br />It is quite possible that large amounts of content will take longer than this to load<br />16<br />
  17. 17. XNA Loading Strategies<br />If you make a game with a large amount of content you should not load it all in the LoadContent method for the game<br />Instead the game should load a splash screen and display that while a background thread loads the content into memory<br />It is important to manage the content loading process in an XNA game and only load what you need at any point in the game<br />17<br />
  18. 18. Summary<br />Applications and games on Windows Phone are given icons to identify them<br />There are images for the application on the Start screen and also in the Application list<br />Silverlight applications also have a splash screen image which is displayed when they start running<br />XNA developers do not have a splash screen provided but games with lots of content may need to display one<br />
  19. 19. Persisting data in isolatedstorage<br />
  20. 20. Topics<br />Windows Phone applications and isolated storage<br />Using isolated storage to store files<br />Storing name/value pairs<br />An introduction to the Dictionary collection class<br />Storing settings information in Isolated Storage<br />
  21. 21. Isolated Storage<br />This storage is called isolated because each application on the phone has its own area<br />One application cannot access the storage of another<br />The data is stored in the mass storage of the phone<br />A program can store very large amounts of data in isolated storage<br />21<br />
  22. 22. Jotpad program<br />This is the first version of the jotpad program<br />It displays a textbox for the user to enter text<br />It also has Load and Save buttons that can be used to load and save the jotted text to isolated storage<br />22<br />
  23. 23. The Save button behaviour<br />privatevoidsaveButton_Click(object sender, RoutedEventArgs e){saveText("jot.txt", jotTextBox.Text);}<br />When the user presses the Save button the event hander calls a method to save the text from the TextBox into a file<br />The saveText method is also given the name of the file to save the text in<br />
  24. 24. The saveText method<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageFile.GetUserStoreForApplication()) {using (IsolatedStorageFileStreamrawStream =isf.CreateFile(filename)) {StreamWriter writer = newStreamWriter(rawStream);writer.Write(text);writer.Close(); } }}<br />The method can be used to save data in a file in isolated storage<br />
  25. 25. Using IsolatedStorage<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageFile.GetUserStoreForApplication()) {using (IsolatedStorageFileStreamrawStream =isf.CreateFile(filename)) {StreamWriter writer = newStreamWriter(rawStream);writer.Write(text);writer.Close(); } }}<br />The method starts by getting a reference to the isolated storage for this application<br />
  26. 26. Creating a file<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageFile.GetUserStoreForApplication()) {using (IsolatedStorageFileStreamrawStream =isf.CreateFile(filename)) {StreamWriter writer = newStreamWriter(rawStream);writer.Write(text);writer.Close(); } }}<br />This reference is then used to create a stream connected to the newly created file<br />
  27. 27. Writing to the file<br />privatevoidsaveText(string filename, string text){using (IsolatedStorageFileisf =IsolatedStorageFile.GetUserStoreForApplication()) {using (IsolatedStorageFileStreamrawStream =isf.CreateFile(filename)) {StreamWriter writer = newStreamWriter(rawStream);writer.Write(text);writer.Close(); } }}<br />The method can now write data to the file and close it <br />
  28. 28. The Load button behaviour<br />privatevoidloadButton_Click(object sender, RoutedEventArgs e){string text;if ( loadText("jot.txt", out text ) ) {jotTextBox.Text = text; }else{jotTextBox.Text = "Type your jottings here...."; }}<br />The load behaviour is more complex because a the file might not be available<br />The load method displays a default message if loadText fails<br />
  29. 29. Loading from Isolated storage<br />try{using (IsolatedStorageFileStreamrawStream =isf.OpenFile(filename, System.IO.FileMode.Open)) {StreamReader reader = newStreamReader(rawStream); result = reader.ReadToEnd();reader.Close();}}catch {returnfalse;}<br />This code reads a file from isolated storage<br />It uses standard file input/output methods<br />
  30. 30. The Isolated Storage File Store<br />Your applications can create many files in isolated storage<br />They can also build up a directory hierarchy within the storage<br />You can perform stream based input/output with files in the isolated storage<br />30<br />
  31. 31. Demo 1: Jotpad Demo<br />Demo<br />31<br />
  32. 32. Using Settings Isolated storage<br />Creating files in isolated storage is useful, but often a program only wants to store name/value pairs<br />Examples of this:<br />Username<br />Home directory<br />Colour and display preferences<br />The Isolated storage in Windows Phone also provides setting storage<br />32<br />
  33. 33. Settings and Dictionaries<br />The settings storage works like a Dictionary collection<br />A Dictionary holds a collection of a particular type which is indexed by key values of another type<br />Programs can look up items based on the value of the key<br />33<br />
  34. 34. A Dictionary example<br />classPerson{publicstring Name;publicstring Address;publicstring Phone;}<br />The Person class holds information about a given person in our system<br />The Person class could contain many more data properties than the ones above<br />We need to store and find Person items<br />
  35. 35. A Dictionary example<br />Dictionary<string, Person> Personnel = newDictionary<string, Person>();<br />This creates a Dictionary called Personnel<br />This holds a collection of Person records that are indexed on a string<br />Generics are used to give the types of the index item and the data stored<br />We could use the name of the person as the index item<br />
  36. 36. Storing in the Dictionary<br />Person p1 = newPerson { Name = "Rob", Address = "His House", Phone = "1234" };Personnel.Add(p1.Name, p1);<br />This creates a Person value and adds it to the dictionary<br />The name value is used as the key<br />The dictionary will not allow duplicate keys<br />
  37. 37. Reading from a Dictionary<br />PersonfindPerson = Personnel["Rob"];<br />We can use a string indexer to locate items in the dictionary<br />The Personnel dictionary will return Person values<br />If the item cannot be found this statement will throw an exception <br />
  38. 38. Checking for Items<br />if (Personnel.ContainsKey("Jim")){// If we get here the dictionary // contains Jim}<br />A Dictionary also provides a method that can be used to test for the existence of particular keys<br />Your code should do this rather than throw exceptions<br />
  39. 39. Dictionaries in action<br />Dictionaries are very useful for storing collections that you want to index on a particular key value<br />The storing and searching is managed very efficiently<br />A system can contain multiple dictionaries to index on different key items<br />A program can also iterate through the dictionary contents<br />39<br />
  40. 40. Dictionaries and Isolated Storage<br />The IsolatedStorageSettings class provides a Dictionary based model for storing and retrieving setting information<br />It stores any object indexed on a string key<br />This makes it very easy to store settings objects<br />Your application must call the “Save” method on the settings object to complete a save<br />40<br />
  41. 41. Saving using settings<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =IsolatedStorageSettings.ApplicationSettings;isolatedStore.Remove(filename);isolatedStore.Add(filename, text);isolatedStore.Save();}<br />This Save method stores a string of text with the supplied name<br />
  42. 42. Getting the isolated store<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =IsolatedStorageSettings.ApplicationSettings;isolatedStore.Remove(filename);isolatedStore.Add(filename, text);isolatedStore.Save();}<br />This statement gets a reference to the settings object<br />
  43. 43. Removing an old version<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =IsolatedStorageSettings.ApplicationSettings;isolatedStore.Remove(filename);isolatedStore.Add(filename, text);isolatedStore.Save();}<br />This removes an existing item with this name<br />Removing does not fail if the item is not there<br />
  44. 44. Saving the data<br />privatevoidsaveText(string filename, stringtext){IsolatedStorageSettingsisolatedStore =IsolatedStorageSettings.ApplicationSettings;isolatedStore.Remove(filename);isolatedStore.Add(filename, text);isolatedStore.Save();}<br />This adds the item and then saves the settings back to the isolated store<br />
  45. 45. Saving items<br />You can save objects other than strings<br />Each object must have a unique name<br />Your program must call the Save method to persist the settings information when it has been added to the settings object<br />45<br />
  46. 46. Reading from settings storage<br />Reading is the reverse of writing<br />Your program must provide the key of the item it wants to load<br />Note that the saved item will be returned in the form of an object reference which your program must cast to the required type<br />The settings storage does not provide a ContainsKey method<br />46<br />
  47. 47. Loading from the setting storage<br />privateboolloadText(string filename, outstring result) {IsolatedStorageSettingsisolatedStore = IsolatedStorageSettings.ApplicationSettings;result = "";try {result = (string)isolatedStore[filename];}catch {returnfalse;}returntrue;}<br />
  48. 48. Managing the loading <br />result = "";try {result = (string) isolatedStore[filename];}catch {returnfalse;}<br />Because it is not possible to check if a setting exists the load method must instead catch the exception that is thrown if a key is not found<br />The loatText method returns false to indicate that the load failed<br />48<br />
  49. 49. Isolated Storage<br />A program can create and use as many files as the application requires<br />It is also possible to create folders within isolated storage so an application can organise data as required<br />The data will be persisted when the application is not running<br />If the application is removed from the phone all its isolated storage is deleted<br />49<br />
  50. 50. Demo 2: Settings Jotpad<br />Demo<br />50<br />
  51. 51. Summary<br />Windows Phone provides “local” storage for applications<br />Data stored is local to an application and not shared with or visible to others<br />There is a local filesystem and a dictionary based setting store which can be used for name/value pairs<br />
  52. 52. Persistingapplicationstate<br />
  53. 53. Topics<br />The Windows Phone process model<br />Understanding “tombstoning”<br />Tombstone events in applications<br />Making use of tombstone events to manage application data storage and persistence<br />
  54. 54. Windows Phone Task Management<br />The Windows Phone platform uses a multi-tasking operating system<br />This allows phone functions to operate simultaneously<br />You can listen to music and read your email<br />However, this multi-tasking ability is not extended to applications that we write<br />54<br />
  55. 55. Single Tasking Applications<br />In the present version of Windows Phone it is not possible to run more than one application at the same time<br />Applications are started and stopped in sequence<br />If the user decides to do something different the currently running application will be stopped<br />55<br />
  56. 56. Why Single Task?<br />Removing multi-tasking stops one application from being affected by another as it runs<br />Background tasks can impact on performance and battery life<br />The limited screen space available to an application makes it hard to manage multiple programs at the same time<br />The Windows Phone design does provide for fast task switching by the user<br />56<br />
  57. 57. The Back and Start buttons<br />The Back button can be used to exit from one program and return to one being used earlier<br />The system maintains a stack of applications to allow navigation in and out of tasks<br />Pressing Start opens up the Start menu so that a new application can be selected<br />It is easy to return to a feature you left by pressing the Start button<br />57<br />
  58. 58. Application Switching<br />This means that an application must be adept at stopping and starting<br />The aim is to present the appearance that the application was never stopped at all<br />When a user returns to an application they were using it should be exactly how they left it<br />The application must store and retrieve its state to achieve this<br />58<br />
  59. 59. Tombstoning<br />Tombstoning is the name given to the act of stopping an application so that a user can start another<br />If the user presses the Start button when an application is running that application will be “tombstoned”<br />It is sent an event to signal that it is about to be removed from memory and has a few seconds to save its state and tidy up<br />59<br />
  60. 60. Tombstones and code design<br />The way that a program responds to tombstone events has an impact on the user experience<br />If a user “abandons” an application by pressing Start they might not expect it to retain data they have entered<br />Start might be used as a quick way of cancelling an activity<br />You need think about this at design time <br />60<br />
  61. 61. Jotpad and tombstones<br />We are going to make the Jotpad program “tombstone friendly”<br />The program will automatically persist data when the user exits and load on entry<br />It will also store data when it is tombstoned<br />Initially we will use the isolated storage<br />To do this we need to add code to the tombstone event methods<br />61<br />
  62. 62. “Tombstone” events<br />There are four “tombstone” events<br />Launching – a new copy of the program has been started by the user<br />Closing- the program is ending normally<br />Deactivated – the user has pressed Start to run another program (tombstone in progress)<br />Activated – the program is being started as a result of the Back button being used<br />62<br />
  63. 63. Tombstone event methods<br />Each of the events is mapped onto a method in the App.xaml.cs file<br />By adding code into these methods an application can get control when the event occurs<br />The code in the method can load or save program status as appropriate<br />63<br />
  64. 64. Tombstone event methods<br />privatevoidApplication_Launching(object sender, LaunchingEventArgse){}privatevoidApplication_Activated(object sender, ActivatedEventArgse){}privatevoidApplication_Deactivated(object sender, DeactivatedEventArgse){}privatevoidApplication_Closing(object sender, ClosingEventArgse){}<br />
  65. 65. Saving on closing<br />privatevoidApplication_Closing(object sender, ClosingEventArgse){MainPagejotPadPage = (MainPage)RootFrame.Content;jotPadPage.Save();}<br />The closing method gets a reference to the mainPage of the application and calls the Save behaviour on that page<br />This removes the need for a Save button<br />Jotpad now saves the text automatically<br />
  66. 66. MainPage Save method<br />publicvoid Save(){saveText("jot.txt", jotTextBox.Text);}<br />This is the Save method in the MainPage for the JotPad application<br />It calls the saveText method to save the text from the textbox<br />It is now public so it can be used from outside the MainPage class<br />
  67. 67. Saving to memory<br />The save methods that we have seen up to now use persistent storage to hold status<br />This can be slow and hard to use if you just want to store the current status of the user interface<br />Windows Phone provides a way that an application can store status information in memory when it is tombstoned<br />67<br />
  68. 68. Saving on tombstoning<br />privatevoidApplication_Deactivated(object sender, DeactivatedEventArgse){MainPagejotPadPage = (MainPage)RootFrame.Content;jotPadPage.SaveState();}<br />The deactivated method looks very similar to the closing method<br />It calls the SaveState method rather than the save method<br />
  69. 69. Saving on tombstoning<br />privatevoidApplication_Deactivated(object sender, DeactivatedEventArgse){MainPagejotPadPage = (MainPage)RootFrame.Content;jotPadPage.SaveState();}<br />The deactivated method looks very similar to the closing method which runs when the application closes<br />It calls the SaveState method rather than the Save method<br />
  70. 70. The SaveStateText method<br />privatevoidSaveStateText (string filename, string text){IDictionary<string, object> stateStore = PhoneApplicationService.Current.State;stateStore.Remove(filename);stateStore.Add(filename,text);}<br />The PhoneApplicationService.Current.Stateobject works as a dictionary where a program can store state information<br />It is in the Microsoft.Phone.Shell namespace <br />
  71. 71. The loadStateText method<br />privateboolloadStateText(string filename, outstring result)<br />{<br />IDictionary<string, object> stateStore = PhoneApplicationService.Current.State;<br />result = "";<br />if (!stateStore.ContainsKey(filename)) returnfalse;<br /> result = (string)stateStore[filename];<br />returntrue;<br />}<br />The loadSaveState method fetches the value from the state storage<br />
  72. 72. loadTextvsloadStateText<br />The loadStateText method is very similar to the loadState method<br />They are both used in exactly the same way<br />One saves to isolated storage<br />One saves to the state object<br />They both have exactly the same signature and can be used interchangeably<br />This reflects good design<br />72<br />
  73. 73. Reloading data when restarted<br />A program can get Launching or Activated messages when it restarts<br />Launching means a new copy is starting<br />Activated means that it is restarted after being tombstoned<br />Unfortunately these events occur before any of the Silverlight user interface components have been created<br />They can’t be used to actually load data<br />73<br />
  74. 74. Loading data on restart<br />The JotPad program tries to load from the state memory first and then loads from isolated storage if this fails<br />This is appropriate behaviour for this application<br />It might not be appropriate for all programs however<br />For some applications the user might want to press Start to abandon their input<br />74<br />
  75. 75. The Load method<br />publicvoid Load()<br />{<br />string text;<br />if (loadStateText("jot.txt", out text)) {<br />jotTextBox.Text = text;<br />return;<br /> }<br />if (loadText("jot.txt", out text)) {<br />jotTextBox.Text = text;<br /> }<br />else<br /> {<br />jotTextBox.Text = "Type your jottings here....";<br /> }<br />}<br />
  76. 76. Loading at program start<br />privatevoidPhoneApplicationPage_Loaded(object sender, RoutedEventArgse)<br />{<br /> Load();<br />}<br />The program can call the Load method when the MainPageis loaded<br />It can then copy all the data content into the user interface elements<br />
  77. 77. Using Visual Studio<br />Visual Studio retains contact with an application when it is tombstoned<br />If you press the Start button on device or Emulator while debugging a program<br />If the application is reactivated Visual Studio will continue debugging<br />This makes it easy to debug the code that manages tombstoning in your applications<br />77<br />
  78. 78. Demo 1: Jotpad Demo<br />Demo<br />78<br />
  79. 79. Summary<br />The Windows Phone operating system uses a single tasking model for applications<br />Start and Back buttons are used by the user to quickly switch between applications<br />Applications can bind to “tombstone” events so that they can save and load their state<br />Windows Phone provides state memory that an application can use to retain data if it is stopped<br />
  80. 80. Launchers and choosers<br />
  81. 81. Topics<br />Launchers and choosers in context<br />Tombstoning and Launchers and Choosers<br />Using a Launcher<br />Starting an application and returning from it<br />Using a Chooser<br />Starting an application and using the result that is returned<br />
  82. 82. Launchers and Choosers<br />Windows Phone provides a way for programs to interact with the phone itself:<br />Take photographs<br />Place phone calls <br />Interact with the address book<br />Select media<br />Now we are going to find out how to do this<br />82<br />
  83. 83. User Involvement<br />Note that in all the applications the user is directly involved and has the final say on the action<br />A program cannot just take a photograph, place a call or send and SMS<br />The user must confirm these operations before they complete<br />An application can only initiate the action<br />83<br />
  84. 84. Launchers vs Choosers<br />Applications call a Launcher makes use of a phone feature<br />Place a phone call, send an email or SMS<br />A Chooser is used to select something<br />Take a picture and return it<br />Select an email address<br />Both are used in the same way, but a Chooser will generate an event that delivers the result<br />84<br />
  85. 85. Calling a Launcher or Chooser<br />When an application calls a Launcher or Chooser the new task gets control<br />When the task is complete the application regains control<br />If the user never returns from the Launcher/Chooser the application never gets control back<br />This when the new task gets control an application may get tombstoned<br />85<br />
  86. 86. Launchers<br />These are the Launchers available:<br />PhoneCallTask<br />EmailComposeTask<br />SmsComposeTask<br />SearchTask<br />WebBrowserTask<br />MediaPlayerLauncher<br />MarketplaceDetailTask<br />MarketplaceHubTask<br />MarketplaceSearchTask<br />86<br />
  87. 87. Using a Launcher<br />As an example, we could add an email feature to the JotPad application<br />This would allow the user to send a jotting as an email<br />When the Mail button is pressed the EmailComposeTask is started<br />87<br />
  88. 88. The Mail button<br />privatevoidmailButton_Click(object sender, RoutedEventArgse){sendMail("From JotPad", jotTextBox.Text);}<br />When the user clicks the mail button the event handler calls the sendMail method<br />This is given the title and text of the email that is to be sent<br />
  89. 89. The Mail button<br />privatevoidsendMail(string subject, string body){EmailComposeTask email = newEmailComposeTask();email.Body = body;email.Subject = subject;email.Show();}<br />The sendMail method creates an EmailComposeTask instance and then calls Show on that instance<br />When the email has been sent the jotPad program will resume<br />
  90. 90. The Tasks namespace<br />usingMicrosoft.Phone.Tasks;<br />In order to use the Launcher and Chooser classes by name an application should add the above namespace<br />Otherwise you will have to use the fully formed version of the class names<br />
  91. 91. Demo 1: Email Jotpad<br />Demo<br />91<br />
  92. 92. Choosers<br />These are the Choosers available:<br />CameraCaptureTask<br />EmailAddressChooserTask<br />PhoneNumberChooserTask<br />PhotoChooserTask<br />SaveEmailAddressTask<br />SavePhoneNumberTask<br />92<br />
  93. 93. Choosers<br />Before an application calls a chooser it can bind to an event that the chooser task generates<br />This is used to deliver a result object to the application when it regains control<br />Choosers must be created in the constructor for a page and declared as members of the page class<br />93<br />
  94. 94. Picture display application<br />The picture display application uses the PhotoChooserTask to allow the user to select a picture for display<br />It then displays this on the phone screen<br />94<br />
  95. 95. Creating the PhotoChooserTask<br />PhotoChooserTaskphotoChooser;<br />publicMainPage()<br />{<br />InitializeComponent();<br />photoChooser = newPhotoChooserTask();<br />photoChooser.Completed+= <br />newEventHandler<PhotoResult>(photoChooser_Completed);<br />}<br />The page constructor creates a PhotoChooserTask and binds a method to the Completed event<br />
  96. 96. The Completed event handler<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskResult.OK)<br /> {<br />selectedImage.Source = newBitmapImage(newUri(e.OriginalFileName));<br /> }<br />}<br />The event handler for the completed event creates a new bitmap image from the filename in the result and displays this<br />
  97. 97. The TaskResult field<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskResult.OK)<br /> {<br />selectedImage.Source = newBitmapImage(newUri(e.OriginalFileName));<br /> }<br />}<br />The TaskResult field in the result is set to TaskResult.OK if the user completed the choose action<br />
  98. 98. The OriginalFileName field<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskResult.OK)<br /> {<br />selectedImage.Source = newBitmapImage(newUri(e.OriginalFileName));<br /> }<br />}<br />The result also contains the filename of the photo that was selected<br />We can use this to create a URI to the image<br />
  99. 99. Create a new image<br />voidphotoChooser_Completed(object sender, PhotoResult e)<br />{<br />if (e.TaskResult == TaskResult.OK)<br /> {<br />selectedImage.Source = newBitmapImage(newUri(e.OriginalFileName));<br /> }<br />}<br />The program can create a new image from the URI and then set the source of the selected image to this<br />
  100. 100. Load button event handler<br />privatevoidloadButton_Click(object sender, RoutedEventArgse){photoChooser.Show();}<br />When the Load button is pressed the event handler just calls the Show method on the chooser that was created in the form constructor<br />
  101. 101. Demo 2: Picture display<br />Demo<br />101<br />
  102. 102. Summary<br />Launchers and Choosers provide a way that applications can use phone features<br />Launchers just start a phone feature running whereas a Chooser can return a result <br />When a Launcher or Chooser is invoked the running application is tombstoned<br />A Chooser will fire an event method in the application when/if it returns<br />

×