• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Windows Phone 7: Navigating Between Pages
 

Windows Phone 7: Navigating Between Pages

on

  • 12,236 views

 

Statistics

Views

Total Views
12,236
Views on SlideShare
12,153
Embed Views
83

Actions

Likes
4
Downloads
0
Comments
2

1 Embed 83

http://windowsapptutorials.wordpress.com 83

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Windows Phone 7: Navigating Between Pages Windows Phone 7: Navigating Between Pages Presentation Transcript

    • Windows  Phone:    Naviga1on  between  Pages   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
    • 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  
    • Silverlight:  for  apps  and  u1li1es  •  Silverlight  apps  are  combina1on  of  code  and   markup  •  Extensible  markup  language  (XAML),   pronounced  “zammel”  
    • 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    
    • Silverlight  files  •  Markup  files  and  “code  behind”  files:   –  App.xaml   –  App.xaml.cs   –  MainPage.xaml   –  MainPage.xaml.cs  
    • 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(); ...
    • 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  
    • 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)); } }}
    • 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>
    • 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(); }}
    • 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”
    • Xap  is  Zip  •  binDebug  dir  has  .xap  file   –  Pronounced  “zap”  •  This  is  the  file  to  be  deployed  to  phone  or   emulator  
    • NAVIGATION  AND  LIFECYCLE  
    • Basic  Structure  •  App  class  starts  the  app  •  PhoneApplicationFrame  instance  can  host   one  or  more  pages  •  PhoneApplicationPage  instances  are  the   pages  
    • MainPage  namespace PhoneApp8{ public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } …}
    • 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();  
    • 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)); } }
    • 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));  
    • RETAIN  DATA  ACROSS  PAGES  
    • 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    
    • Page  1  to  Page  2  •  Pass  values  like  in  Web!   –   NavigationService.Navigate(new Uri("/ SecondPage.xaml?name=Jussi", UriKind.Relative));
    • 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); }
    • 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); }
    • 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  
    • 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! *"); }
    • 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  
    • 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); }
    • 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  
    • 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.    
    • Common  value  for  All  public partial class App : Application { private String commonValue = "Default"; public String CommonValue { set { commonValue = value; } get { return commonValue; } }
    • Usage  of  Common  Value  private void button1_Click(object sender, RoutedEventArgs e) { App appRef = Application.Current as App; PageTitle.Text = appRef.CommonValue; }
    • 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"]; }
    • 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();}