Navigation in Windows Phone 7

1 ) All the contents in the xaml Page should be places inside the        TAG

    for Windows Phone

This code will contain 5 Files :

1 ) NavigatePage.xaml

2 ) Navigatepage.xaml.cs

3 ) NavigatepageViewModal.cs

4 ) Navigationutility.cs

5 ) ButtonClickCommand.cs

——————*—————————

Code for NavigatePage.xaml

* This page contains a button whose click event will direct you to another page *


        

        

        
    
 

------------------*---------------------------

Code for NavigatePage.xaml.cs

* provide the datacontext for the entire page in the page constructor *


//constructor
public  NavigatePage ()
        {
            InitializeComponent();
            //provide data context
            this.DataContext = new NavigateViewModal();

           
        }

 
------------------*---------------------------


Code for NavigatepageViewModal.cs

* this is the view modal for the NavigatePage which helps to register the button click binding *


public class NavigatepageViewModal
    {

        public ButtonClickCommand LoginCommand { get; private set; }


        public  NavigatepageViewModal ()
        {
            LoginCommand = new ButtonClickCommand(UserValidate);
            LoginCommand.IsEnabled = true;
        }


        /// 
        /// This function is calling the service
        /// and passing username and userpassword
        /// 
        private void UserValidate()
        {
         //redirect the NavigatePage  to MainPage.xaml(Or any custom Page) using the          //methods of Navigationutility
            new  Navigationutility().Navigate("/MainPage.xaml");
        }

    }

------------------*---------------------------


Code for  ButtonClickCommand.cs

* This class registers the method to be executed when button is clicked and call the navigation service to navigate *


public class ButtonClickCommand : ICommand
    {
        private readonly Action handler;
        private bool isEnabled;

        public ButtonClickCommand(Action handler)
        {
            this.handler = handler;
        }

//checks if button execution has been set to true
        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (value != isEnabled)
                {
                    isEnabled = value;
                    if (CanExecuteChanged != null)
                    {
                        CanExecuteChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

//method to return if click event enabled property
        public bool CanExecute(object parameter)
        {
            return IsEnabled;
        }

        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter)
        {
            handler();
        }
    }


------------------*---------------------------
 

Code for Navigationutility.cs
 
*This page contains the code for navigation to another frame*


        //create a phone application frame to get the main frame
        private PhoneApplicationFrame _mainFrame;

        //For navigating event which can be cancelled
        public event NavigatingCancelEventHandler Navigating;

        /// 
        /// method to take uri and navigate to 
        /// the specified page
        /// 
        /// Uri
        public void NavigateTo(Uri pageUri)
        {
            if (EnsureMainFrame())
            {
//navigates the main frame(PhoneApplicationFrame ) to the uri passed 
                _mainFrame.Navigate(pageUri);
            }
        }

        //checks and returns if main frame present
//else sets the main frame
        private bool EnsureMainFrame()
        {
            if (_mainFrame != null)
            {
                return true;
            }

//gets the main frame for the current app
            _mainFrame = Application.Current.RootVisual as PhoneApplicationFrame;

            if (_mainFrame != null)
            {
                // Could be null if the app runs inside a design tool
                _mainFrame.Navigating += (s, e) =>
                {
                    if (Navigating != null)
                    {
                        Navigating(s, e);
                    }
                };

                return true;
            }

            return false;
        }


//this method is called from other view modal classes
        public void Navigate(string url)
        {
            NavigateTo(new Uri(url, UriKind.Relative));
        }

------------------*---------------------------

The flow of the entire code would be like

1 ) NavigatePage.xaml

2 ) Navigatepage.xaml.cs

3 ) NavigatepageViewModal.cs

4 ) Navigationutility.cs

5 ) ButtonClickCommand.cs

NavigatePage.xaml ->

Navigatepage.xaml.cs( calls NavigatepageViewModal while assigning datacontext to entire page) ->

NavigatepageViewModal.cs ( registers the button click by calling ButtonClickCommand  and  calls the methods of the Navigationutility to navigate)

->

ButtonClickCommand.cs (registers the method to be executed in NavigatepageViewModal when button click is performed)->

Navigationutility.cs (Directs the page in the phone application frame to the other page)->

MainPage.xaml( or your custom page )

150 150 Burnignorance | Where Minds Meet And Sparks Fly!