Problem to close or minimize app for Windows Phone 8.1 in C #

0

I have been creating an application in Visual Studio 2015 for Windows Phone 8.1 in the C # language. I have created two pages, Main Page and Page 2. When I am in Page 2 and when I clicked the emulator on the back button, I minimized the application. I added Using windows.phone.ui.input and this:

using System; 
using System.Collections.Generic; 
using System.IO; using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using Windows.Phone.UI.Input; 
// La plantilla de elemento Página en blanco está documentada en http://go.microsoft.com/fwlink/?LinkID=390556

namespace Cuentos_Infantiles {
    /// <summary>
    /// Página vacía que se puede usar de forma independiente o a la que se puede navegar dentro de un objeto Frame.
    /// </summary>
    public sealed partial class BlankPage1 : Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();
        }

        public object NavigationService { get; private set; }

        /// <summary>
        /// Se invoca cuando esta página se va a mostrar en un objeto Frame.
        /// </summary>
        /// <param name="e">Datos de evento que describen cómo se llegó a esta página.
        /// Este parámetro se usa normalmente para configurar la página.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            Frame.Navigate(typeof(MainPage));
            e.Handled = true;
        }

        private void Contenido1_SelectionChanged(object sender, RoutedEventArgs e)
        {


        }

    } 
}

and when debugging again, return to the previous page as I want. But it turns out that being on Main Page (home page), the back button does not respond or minimize or close the application.

This is the Main Page code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.Phone.UI;
using Windows.UI.Xaml.Navigation;

// La plantilla de elemento Página en blanco está documentada en http://go.microsoft.com/fwlink/?LinkId=391641

namespace Cuentos_Infantiles
{
    /// <summary>
    /// Página vacía que se puede usar de forma independiente o a la que se puede navegar dentro de un objeto Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Se invoca cuando esta página se va a mostrar en un objeto Frame.
        /// </summary>
        /// <param name="e">Datos de evento que describen cómo se llegó a esta página.
        /// Este parámetro se usa normalmente para configurar la página.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Preparar la página que se va a mostrar aquí.

            // TODO: Si la aplicación contiene varias páginas, asegúrese de
            // controlar el botón para retroceder del hardware registrándose en el
            // evento Windows.Phone.UI.Input.HardwareButtons.BackPressed.
            // Si usa NavigationHelper, que se proporciona en algunas plantillas,
            // el evento se controla automáticamente.
        }

        private void Hyperlinkbutton_101_dalmatas_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BlankPage1));
        }
    }
}

Please, I need help because I just want to minimize the application like the others by clicking on the back button.

Am I missing some code or what am I doing wrong?

    
asked by Alexis Herrera Bilora 22.01.2018 в 23:57
source

1 answer

0

I have not programmed for Windows Phone a good time, but I would tell you that the problem is here:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame.Navigate(typeof(MainPage));
    e.Handled = true;
}

In OnNavigatedTo you are indicating that when you press the back button, you should call HardwareButtons_BackPressed where what is done is that you go to the Main Page and the event is canceled (with e.Handled = true ).

That code should not go in the code of (or of any page), but in the general code of the application (App.xaml.cs) because you are indicating a global behavior (when you press back, navigate to the Main Page) and also because you run the risk of being added several times (you are doing += ) and the behavior and performance of your application will be affected.

Then, there you are only controlling that you go back to the Main Page and no other action is taken, but since it is a global event, that will also happen when you are already on the Main Page ... staying in an application without output (the problem described).

You must add something more logical. You would need a if to check whether you are on the Main Page or not. Something like this example (based on this another Stack Overflow in English ):

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)

    // obtenemos el frame actual
    Frame frame = Window.Current.Content as Frame;

    // si se puede ir atrás, entonces ir atrás y parar los eventos
    if (frame.CanGoBack)
    {
        frame.GoBack();
        e.Handled = true;
    }
}
    
answered by 23.01.2018 / 00:54
source