/// <summary>
        /// Method called when the page is leaved.
        /// </summary>
        protected override void OnNavigatedFrom(NavigationEventArgs nee)
        {
            if (((InCallModel)ViewModel).IsVideoActive)
            {
                LinphoneCall call = null;
                try
                {
                    call = (LinphoneCall)LinphoneManager.Instance.LinphoneCore.Calls[0];
                }
                catch (System.ArgumentOutOfRangeException) { }
                if (call != null)
                {
                    ApplicationSettingsManager settings = new ApplicationSettingsManager();
                    settings.Load();
                    settings.VideoActiveWhenGoingToBackground = true;
                    settings.VideoAutoAcceptWhenGoingToBackground = LinphoneManager.Instance.LinphoneCore.VideoPolicy.AutomaticallyAccept;
                    settings.Save();
                    LinphoneManager.Instance.LinphoneCore.VideoPolicy.AutomaticallyAccept = false;
                    LinphoneCallParams callParams = call.GetCurrentParamsCopy();
                    callParams.VideoEnabled = false;
                    LinphoneManager.Instance.LinphoneCore.UpdateCall(call, callParams);
                }
            }

            oneSecondTimer.Stop();
            if (fadeTimer != null)
            {
                fadeTimer.Dispose();
                fadeTimer = null;
            }

            base.OnNavigatedFrom(nee);
            this.ViewModel.MuteListener = null;
            this.ViewModel.PauseListener = null;
            LinphoneManager.Instance.CallStateChanged -= CallStateChanged;
            AudioRoutingManager.GetDefault().AudioEndpointChanged -= AudioEndpointChanged;
        }
        /// <summary>
        /// Method called when the page is displayed.
        /// Searches for a matching contact using the current call address or number and display information if found.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs nee)
        {
            base.OnNavigatedTo(nee);
            this.ViewModel.MuteListener = this;
            this.ViewModel.PauseListener = this;
            this.ViewModel.CallUpdatedByRemoteListener = this;
            LinphoneManager.Instance.CallStateChanged += CallStateChanged;
            AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged;

            if (NavigationContext.QueryString.ContainsKey("sip"))
            {
                String calledNumber = NavigationContext.QueryString["sip"];
                LinphoneAddress address = LinphoneManager.Instance.LinphoneCore.InterpretURL(calledNumber);
                calledNumber = String.Format("{0}@{1}", address.UserName, address.Domain);
                // While we dunno if the number matches a contact one, we consider it won't and we display the phone number as username
                Contact.Text = calledNumber;

                if (calledNumber != null && calledNumber.Length > 0)
                {
                    ContactManager cm = ContactManager.Instance;
                    cm.ContactFound += cm_ContactFound;
                    cm.FindContact(calledNumber);
                }
            }

            ApplicationSettingsManager settings = new ApplicationSettingsManager();
            settings.Load();

            // Callback CallStateChanged set too late when call is incoming, so trigger it manually
            if (LinphoneManager.Instance.LinphoneCore.CallsNb > 0)
            {
                LinphoneCall call = (LinphoneCall)LinphoneManager.Instance.LinphoneCore.Calls[0];
                if (call.State == LinphoneCallState.StreamsRunning)
                {
                    CallStateChanged(call, LinphoneCallState.StreamsRunning);
                    if (settings.VideoActiveWhenGoingToBackground)
                    {
                        LinphoneManager.Instance.LinphoneCore.VideoPolicy.AutomaticallyAccept = settings.VideoAutoAcceptWhenGoingToBackground;
                        LinphoneCallParams callParams = call.GetCurrentParamsCopy();
                        callParams.VideoEnabled = true;
                        LinphoneManager.Instance.LinphoneCore.UpdateCall(call, callParams);
                    }
                }
                else if (call.State == LinphoneCallState.UpdatedByRemote)
                {
                    // The call was updated by the remote party while we were in background
                    LinphoneManager.Instance.CallState(call, call.State, "call updated while in background");
                }
            }

            settings.VideoActiveWhenGoingToBackground = false;
            settings.Save();

            oneSecondTimer = new DispatcherTimer();
            oneSecondTimer.Interval = TimeSpan.FromSeconds(1);
            oneSecondTimer.Tick += timerTick;
            oneSecondTimer.Start();
        }