Oxygene and WP8 – Navigation in Windows Phone 8

The Windows Phone 8 SDK provides the Navigate method defined in the NavigationService which lets the developers to provide ability for the user to navigate from one page to another.

For example , imagine you have a page called “menu.xaml” which contains the list of menu within your application . On clicking of one of the menu button , the user needs to navigate to a different page . In this case , you can use the NavigationService to navigate from one page to another.

Simple Navigation in Windows Phone 8

In this example , let’s assume there are 2 pages “MainPage1.xaml” and “MainPage2.xaml” . The MainPage1.xaml has a button and on click of it the page has to be navigated from MainPage1.xaml to MainPage2.xaml.

method MainPage.Button_Click_1(sender: Object; e: System.Windows.RoutedEventArgs);

begin

   NavigationService.Navigate(new Uri("/MainPage2.xaml", UriKind.Relative));

end;

The NavigationService.Navigate method requires the Uri of the page to which the NavigationService has to navigate to.

Alternatively , the NavigationService also includes the property called Source which also provides the same behaviour as the Navigate method.

NavigationService.Source := new Uri("/MainPage2.xaml", UriKind.Relative);

There are different Navigation Lifecycle events of a Windows Phone page . These includes

  • OnNavigatingTo – This is a method that gets called when the page becomes the current active page in the frame. When you want to load the data from the Isolated storage etc , you would use these method .
  • OnNavigatingFrom – This is a method that will be called just before the current page is no longer the active page in the frame . Typically when you want to cancel the navigation , this method can be used.
  • OnNavigatedFrom – This is a method that will be called after the page is no longer rge active page in the frame . If you want to save the data in the isolated storage etc for the page , you would use this method

You can access these events by overriding the corresponding methods as shown below.

namespace MobileOSGeekOxygeneTutorial;

interface

uses
  System,
  System.Collections.Generic,
  System.Linq,
  System.Net,
  System.Windows,
  System.Windows.Controls,
  System.Windows.Navigation,
  Microsoft.Phone.Controls,
  Microsoft.Phone.Shell;

type
  MainPage2 = public partial class(PhoneApplicationPage)
  public
    constructor ;

  protected
    method OnNavigatedFrom(e: NavigationEventArgs); override;

    method OnNavigatingFrom(e: NavigatingCancelEventArgs); override;

    method OnNavigatedTo(e: NavigationEventArgs); override;
  end;

implementation
constructor MainPage2;
begin
  InitializeComponent()
end;

method MainPage2.OnNavigatedFrom(e: NavigationEventArgs);
begin
  inherited OnNavigatedFrom(e);
end;

method MainPage2.OnNavigatingFrom(e: NavigatingCancelEventArgs);
begin
  inherited OnNavigatingFrom(e);
  MessageBox.Show("Navigating From");
end;

method MainPage2.OnNavigatedTo(e: NavigationEventArgs);
begin
  inherited OnNavigatedTo(e);
  MessageBox.Show("Navigated To");
end;

end.

Download the sample sourcecode used in this article here

No Responses

Leave a Reply