﻿

***************************** STEPS TO FOLLOW DURING CREATION OF 'PAGE' *******************************

STEP 1:
Any page that we create in this project solution should be of type 'IVMAPage'. So, we have to Implement all properties of IVMAPage.

STEP 2:
Copy and paste below Code and do necessary changes wherever we have marked ---->(%000%)


START CODE:


    #region Page Initializer

		--------------->(%000%) /* Change Name to Corresponding 'Page' or 'UserControl' */
        public CONSTRUCTOR() 
        {
            this.InitializeComponent();			
            this.DataContextChanged += ConversationDetailedPage_DataContextChanged;
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
            InitilizeLocalComponent();           
        }

        private void ConversationDetailedPage_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
        {
            this.OnDataContextChanged(args.NewValue);
        }

        #region Navigation Helper Implementation

        private NavigationHelper navigationHelper;
        /// <summary>
        /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
        /// <param name="e">Event data that provides an empty dictionary to be populated with
        /// serializable state.</param>
        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }

        #region NavigationHelper registration

        /// <summary>
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// <para>
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="NavigationHelper.LoadState"/>
        /// and <see cref="NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.
        /// </para>
        /// </summary>
        /// <param name="e">Provides data for navigation methods and event
        /// handlers that cannot cancel the navigation request.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        #endregion

        #region IVMAPage
        public void InitilizeLocalComponent()
        {
				--------------->(%000%) /* Change Name to Corresponding 'ViewModel' */
			  this.DataContext = App.GetDataContextVM(Global.Common.Enums.VMType.ConversationDetailedVM) as ViewModel.Conversation.ConversationDetailViewModel;
        }
        
        public void OnDataContextChanged(object datacontext) 
        {
            /* NOTE: 
               1. You could assign any ViewModel of type IViewModel any number of times 
               2. you are allowed to change datacontext on runtime based on requirement!!
               */

            if (null != datacontext && this.ViewModelDataContext != datacontext)
            {
                this.ViewModelDataContext = datacontext;
            }
        }

        private Global.Common.Enums.VMType _VMType;
        public Global.Common.Enums.VMType VMType
        {
            get
            {
                return _VMType;
            }
            private set
            {
                _VMType = value;
            }
        }

        public object ViewModelDataContext
        {
           get
            {
				--------------->(%000%) /* Change VMType to Corresponding 'ViewModel' */

                if (VMType == Global.Common.Enums.VMType.ConversationDetailedVM)
                {
                    return this.ConversationVMObj;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (!(value is IViewModel)) throw new InvalidCastException("Value that you are trying to set is not of required 'Type'.");

                /* NOTE: 
                1. You could assign any ViewModel of type IViewModel any number of times 
                2. you are allowed to change datacontext on runtime based on requirement!!
                */

				--------------->(%000%) /* Change VMType to Corresponding 'ViewModel' */
                if (value is ViewModel.Conversation.ConversationMainViewModel)
                {
                    this.VMType = Global.Common.Enums.VMType.ConversationMainVM;

                    this.ConversationVMObj = value as ViewModel.Conversation.ConversationMainViewModel;

                    this.ConversationVMObj.PropertyChanged -= ConversationVMObj_PropertyChanged;
                    this.ConversationVMObj.PropertyChanged += ConversationVMObj_PropertyChanged;

                    this.listBoxMessages.ItemsSource = this.ConversationVMObj.MessageObservableList;
                }
                else
                {
                    throw new InvalidCastException("Value that you are trying to set is not of required 'Type'.");
                }
            }
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void RegisterHardwareEvents()
        {
            if (ApiAssistance.IsHardwareButtonsPresent)
            {
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            }
        }

        private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            throw new NotImplementedException();
        }

        public void UnRegisterHardwareEvents()
        {
            if (ApiAssistance.IsHardwareButtonsPresent)
            {
                Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
            }
        }

		
        public void OnViewSizeChanged(Size previousSize, Size newSize)
        {
            throw new NotImplementedException();
        }

        #endregion 
        #endregion



END CODE


***************************** CONGRADULATIONS YOU HAVE CREATED 'IVMAPage' *******************************