Windows	  Phone:	  	  Naviga1on	  between	  Pages	              Jussi	  Pohjolainen	  Tampere	  University	  of	  Applied	...
Programming	  Environments	  •  Two	  programming	  environments	      –  XNA	  –	  for	  games	      –  Silverlight	  –	 ...
Silverlight:	  for	  apps	  and	  u1li1es	  •  Silverlight	  apps	  are	  combina1on	  of	  code	  and	     markup	  •  Ex...
XNA:	  for	  games	  •  High	  performance	  games:	  2D	  and	  3D	  •  Sprites	  and	  backgrounds	  based	  on	  bitmap...
Silverlight	  files	  •  Markup	  files	  and	  “code	  behind”	  files:	     –  App.xaml	     –  App.xaml.cs	     –  MainPag...
App.xaml.cs:	  	         App	  wide	  init,	  startup	  and	  shutdown	  namespace PhoneApp8{    public partial class App ...
App.xaml	  •  App.xaml	  and	  App.xaml.cs	  files	  together	  are	     the	  App-­‐class	  •  How	  can	  .xaml	  file	  b...
App.g.cs	  namespace PhoneApp8 {    public partial class App : System.Windows.Application {        private bool _contentLo...
App.xaml	  <Application    x:Class="PhoneApp8.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   ...
MainPage.xaml.cs	  •  When	  app	  is	  ini1alized	  the	  App	  class	  opens	  the	  Main	  Page	  •  Again	  par1al	  c...
MainPage.cs	  •  Contains	  visual	  elements	  of	  the	  main	  page	  PhoneApplicationFrame  PhoneApplicationPage    Gr...
Xap	  is	  Zip	  •  binDebug	  dir	  has	  .xap	  file	      –  Pronounced	  “zap”	  •  This	  is	  the	  file	  to	  be	  d...
NAVIGATION	  AND	  LIFECYCLE	  
Basic	  Structure	  •  App	  class	  starts	  the	  app	  •  PhoneApplicationFrame	  instance	  can	  host	     one	  or	 ...
MainPage	  namespace PhoneApp8{    public partial class MainPage : PhoneApplicationPage    {        // Constructor        ...
Navigate	  •  Naviga1on	  is	  based	  on	  XAML-­‐files	  (like	  .html	     files	  in	  Web)	  •  Use	  Naviga1on	  Servi...
public partial class MainPage : PhoneApplicationPage    {        // Constructor        public MainPage()        {         ...
About	  Naviga1on	  •  Last-­‐in-­‐first-­‐out	  =>	  Stack	  •  Phones	  back	  bueon	  is	  the	  same	  than	      –  Na...
RETAIN	  DATA	  ACROSS	  PAGES	  
Possibili1es	  •  Every	  1me	  you	  navigate	  to	  new	  page,	  a	  new	     instance	  is	  created	  =>	  data	  los...
Page	  1	  to	  Page	  2	  •  Pass	  values	  like	  in	  Web!	      –  	  NavigationService.Navigate(new Uri("/       Sec...
To	  receive	  the	  parameters	   protected override voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  ...
Give	  values	  back	  protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)       {   ...
Basics	  •  When	  app	  is	  opened	  from	  Start	  screen	      –  App	  is	  launched	  •  When	  app	  is	  terminate...
public partial class App : Application   {       ...       private void Application_Launching(object sender, LaunchingEven...
PhoneApplica1onService:	        Store	  Data	  while	  App	  in	  Memory	  •  You	  can	  use	  PhoneApplica1onService	  t...
Example	   protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)        {            Ph...
When	  values	  are	  lost?	  •  Open	  app,	  press	  back	  -­‐>	  app	  is	  closed	  -­‐>	  data	     lost	  •  Open	 ...
Persistant	  Storage:	  Isolated	  Storage	  •  Load	  seings	  when	  app	  launching	  or	  ac1va1ng	  •  Save	  seings	...
Common	  value	  for	  All	  public partial class App : Application   {      private String commonValue = "Default";      ...
Usage	  of	  Common	  Value	  private void button1_Click(object sender, RoutedEventArgs e)       {           App appRef = ...
App.xaml.cs:	  void SaveSettings()   {       IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSetting...
App.xaml.cs:	  private void Application_Launching(object sender, LaunchingEventArgs e){    LoadSettings();}private void Ap...
Windows Phone 7: Navigating Between Pages
Upcoming SlideShare
Loading in …5
×

Windows Phone 7: Navigating Between Pages

13,702 views

Published on

Published in: Technology

Windows Phone 7: Navigating Between Pages

  1. 1. Windows  Phone:    Naviga1on  between  Pages   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
  2. 2. Programming  Environments  •  Two  programming  environments   –  XNA  –  for  games   –  Silverlight  –  non-­‐games  •  You  can  mix  some  of  the  libraries  •  When  crea1ng  a  project,  you  must  decide  if   you  are  doing  a  XNA  or  Silverlight  
  3. 3. Silverlight:  for  apps  and  u1li1es  •  Silverlight  apps  are  combina1on  of  code  and   markup  •  Extensible  markup  language  (XAML),   pronounced  “zammel”  
  4. 4. XNA:  for  games  •  High  performance  games:  2D  and  3D  •  Sprites  and  backgrounds  based  on  bitmaps  •  Moving  objects  around  screen,  polling  user   input,  built-­‐in  XNA  loop    
  5. 5. Silverlight  files  •  Markup  files  and  “code  behind”  files:   –  App.xaml   –  App.xaml.cs   –  MainPage.xaml   –  MainPage.xaml.cs  
  6. 6. App.xaml.cs:     App  wide  init,  startup  and  shutdown  namespace PhoneApp8{ public partial class App : Application { Par1al  class!  So   public App() another  App-­‐ class  must  be   { somewhere!   ... InitializeComponent(); ...
  7. 7. App.xaml  •  App.xaml  and  App.xaml.cs  files  together  are   the  App-­‐class  •  How  can  .xaml  file  be  part  of  .cs?   –  During  compila1on   •  App.xaml  -­‐>  App.g.cs  !   •  g  stands  for  generated  
  8. 8. App.g.cs  namespace PhoneApp8 { public partial class App : System.Windows.Application { private bool _contentLoaded; [System.Diagnostics.DebuggerNonUserCodeAttribute()] public void InitializeComponent() { if (_contentLoaded) { return; } _contentLoaded = true; System.Windows.Application.LoadComponent(this, new System.Uri("/PhoneApp8;component/App.xaml", System.UriKind.Relative)); } }}
  9. 9. App.xaml  <Application x:Class="PhoneApp8.App" 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"> <!--Application Resources--> <Application.Resources> Store   </Application.Resources> resources:   colors,  styles..       <Application.ApplicationLifetimeObjects> <!--Required object that handles lifetime events for the application--> <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated"/> </Application.ApplicationLifetimeObjects></Application>
  10. 10. MainPage.xaml.cs  •  When  app  is  ini1alized  the  App  class  opens  the  Main  Page  •  Again  par1al  class!  The  other  half  is  in  MainPage.xaml.cs    namespace PhoneApp8{ public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); }}
  11. 11. MainPage.cs  •  Contains  visual  elements  of  the  main  page  PhoneApplicationFrame PhoneApplicationPage Grid named “LayoutRoot” StackPanel named “TitlePanel” TextBlock named “ApplicationTitle” TextBlock named “PageTitle” Grid named “ContentPanel”
  12. 12. Xap  is  Zip  •  binDebug  dir  has  .xap  file   –  Pronounced  “zap”  •  This  is  the  file  to  be  deployed  to  phone  or   emulator  
  13. 13. NAVIGATION  AND  LIFECYCLE  
  14. 14. Basic  Structure  •  App  class  starts  the  app  •  PhoneApplicationFrame  instance  can  host   one  or  more  pages  •  PhoneApplicationPage  instances  are  the   pages  
  15. 15. MainPage  namespace PhoneApp8{ public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } …}
  16. 16. Navigate  •  Naviga1on  is  based  on  XAML-­‐files  (like  .html   files  in  Web)  •  Use  Naviga1on  Service  to  navigate   –  Naviga1onService.Navigate(new  Uri("/ SecondPage.xaml",  UriKind.Rela1ve));   –  Naviga1onService.GoBack();  
  17. 17. public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } private void navigateButton_Click(object sender,RoutedEventArgs e) { NavigationService.Navigate(new Uri("/SecondPage.xaml",UriKind.Relative)); } }
  18. 18. About  Naviga1on  •  Last-­‐in-­‐first-­‐out  =>  Stack  •  Phones  back  bueon  is  the  same  than   –  Naviga1onService.GoBack();  •  To  create  new  instance  of  the  page:   –  Naviga1onService.Navigate(new  Uri("/ MainPage.xaml",  UriKind.Rela1ve));  
  19. 19. RETAIN  DATA  ACROSS  PAGES  
  20. 20. Possibili1es  •  Every  1me  you  navigate  to  new  page,  a  new   instance  is  created  =>  data  loss  •  Solu1ons   –  1)  Just  pass  data  across  pages   –  2)  PhoneApplica1onService:  data  in  memory  while   app  running   –  3)  IsolatedStorage:  persistant  storage    
  21. 21. Page  1  to  Page  2  •  Pass  values  like  in  Web!   –   NavigationService.Navigate(new Uri("/ SecondPage.xaml?name=Jussi", UriKind.Relative));
  22. 22. To  receive  the  parameters   protected override voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { IDictionary<string, string> parameters =NavigationContext.QueryString; if (parameters.ContainsKey("name")) { PageTitle.Text = parameters["name"]; } base.OnNavigatedTo(e); }
  23. 23. Give  values  back  protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { if (e.Content is MainPage) { MainPage reference = (e.Content as MainPage); reference.PageTitle.Text = "And we are back!"; } base.OnNavigatedFrom(e); }
  24. 24. Basics  •  When  app  is  opened  from  Start  screen   –  App  is  launched  •  When  app  is  terminated  as  result  of  Back   –  App  is  closed  •  When  program  is  running  and  user  presses   start   –  App  is  deac1vated  -­‐>  tombstoned  state  •  When  program  is  navigated  back   –  App  is  ac1vated  -­‐>  back  from  tombstoned  state  
  25. 25. public partial class App : Application { ... private void Application_Launching(object sender, LaunchingEventArgs e) { Debug.WriteLine("* Launch! *"); } private void Application_Activated(object sender, ActivatedEventArgs e) { Debug.WriteLine("* Activate! *"); } private void Application_Deactivated(object sender, DeactivatedEventArgs e) { Debug.WriteLine("* Deactivate! *"); } private void Application_Closing(object sender, ClosingEventArgs e) { Debug.WriteLine("* Closed! *"); }
  26. 26. PhoneApplica1onService:   Store  Data  while  App  in  Memory  •  You  can  use  PhoneApplica1onService  to  store   data  in  RAM  while  app  is  in  use  or  tombstoned  •  Really  easy  to  implement  store  key  value  pairs   in  PhoneApplica1onService.Current.State  
  27. 27. Example   protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { PhoneApplicationService.Current.State["key"] = "arvo"; base.OnNavigatedFrom(e); } protected override voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey("key")) { string value = (string) PhoneApplicationService.Current.State["key"]; } base.OnNavigatedTo(e); }
  28. 28. When  values  are  lost?  •  Open  app,  press  back  -­‐>  app  is  closed  -­‐>  data   lost  •  Open  app,  press  start  -­‐>  press  back  -­‐>  data  is   there  •  Open  app,  press  start  -­‐>  open  app  again  from   menu  -­‐>  data  lost  •  Open  app,  long  press  back  -­‐>  switch  apps  -­‐>   long  press  back  to  switch  back  -­‐>  data  is  there  
  29. 29. Persistant  Storage:  Isolated  Storage  •  Load  seings  when  app  launching  or  ac1va1ng  •  Save  seings  when  app  deac1va1ng  or  closing  •  Why?   –  program  should  also  save  applica1on  seings  during   the  Deac%vated  event  because  the  program  really   doesn’t  know  if  it  will  ever  be  resurrected.  And  if  it  is   resurrected,  it  should  load  applica1on  seings  during   the  Ac%vated  event  because  otherwise  it  won’t  know   about  those  seings.    
  30. 30. Common  value  for  All  public partial class App : Application { private String commonValue = "Default"; public String CommonValue { set { commonValue = value; } get { return commonValue; } }
  31. 31. Usage  of  Common  Value  private void button1_Click(object sender, RoutedEventArgs e) { App appRef = Application.Current as App; PageTitle.Text = appRef.CommonValue; }
  32. 32. App.xaml.cs:  void SaveSettings() { IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings["savedValue"] = CommonValue; settings.Save(); } void LoadSettings() { string mydefault = "0"; IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if(settings.TryGetValue<string>("savedValue", out mydefault)) CommonValue = (string) settings["savedValue"]; }
  33. 33. App.xaml.cs:  private void Application_Launching(object sender, LaunchingEventArgs e){ LoadSettings();}private void Application_Activated(object sender, ActivatedEventArgs e){ LoadSettings();}private void Application_Deactivated(object sender, DeactivatedEventArgs e){ SaveSettings();}private void Application_Closing(object sender, ClosingEventArgs e){ SaveSettings();}

×