简体   繁体   中英

UWP Navigation page timer

I have a problem with the navigation of my pages, when I change page and I come back on my MainPage my timer NiveauTimer didn't works (I have nothings appears in my TextBox) but the timer DispatcherTimer works well.

namespace BassinExpertV1
{
    public sealed partial class MainPage : Page
    {
        PCF8591 ADConverter;
        DispatcherTimer dispatcherTimer;
        DispatcherTimer NiveauTimer;


        public MainPage()
        {


            //Create Timer Date
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            //Create Timer water level
            NiveauTimer = new DispatcherTimer();
            NiveauTimer.Tick += NiveauTimer_Tick;
            NiveauTimer.Interval = new TimeSpan(0, 0, 1);


            this.InitializeComponent();


        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            dispatcherTimer.Start();    //Timer Date
            NiveauTimer.Start();       //Timer water level
        }




        //----------------------------------------------------------------------------
        //                            Timer Date
        //----------------------------------------------------------------------------
        private void DispatcherTimer_Tick(object sender, object e)
        {
            DateHeure.Text = DateTime.Now.ToString();
        }



        //----------------------------------------------------------------------------
        //                            Timer WaterLevel
        //----------------------------------------------------------------------------
        private async void NiveauTimer_Tick(object sender, object e)
        {
            RecupNiveauAsync();
            await System.Threading.Tasks.Task.Delay(2000); //wait for 2 seconds (= 2000ms)

            try
            {

                double value = ADConverter.ReadI2CAnalog_AsDouble(PCF8591_AnalogPin.A0) *5 + 95; // Conversion du la valeur du potentiomètre
                value = Math.Round(value, 2, MidpointRounding.AwayFromZero); //Arrondir la valeur 
                TextBoxNiveau.Text = Convert.ToString(value) + " %"; // Afficher dans la Textbox la valeur du potentiomètre en %
            }
            catch
            {
                MessageDialog msg = new MessageDialog("Probleme");
                await msg.ShowAsync();
            }


        }
        //----------------------------------------------------------------------------
        //                            Water Level conversion        //----------------------------------------------------------------------------
        private async System.Threading.Tasks.Task RecupNiveauAsync()
        {
            ADConverter = await PCF8591.Create();

        }


        //----------------------------------------------------------------------------
        //                            Bouton automatique
        //----------------------------------------------------------------------------
        private void Automatique_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Automatique), null); //navigation vers la page automatique
        }


        //----------------------------------------------------------------------------
        //                            Bouton manuel
        //----------------------------------------------------------------------------
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Manuel)); //navigation vers la page manuel
        }



        private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}

The problem is that when you navigate from a page and back to it, a new instance of the page is created. The existing page instance stays in memory (because of the timers), but the page you actually see is a new instance of the page, which creates its own timers.

One solution is to set NavigationCacheMode="Required" to your page, so that when you navigate from it and page, it will reuse the existing instance.

You could also make the Timer variables as static and create only a single instance of them.

This leads to the best solution - using a ViewModel class that would contain the Timers and other data and keep the ViewModels for pages on navigation stack in memory. A MVVM framework can help a lot, I suggest using MvvmLight, Prism or MvvmCross - all will handle this logic for you.

Instead of setting up timers in your constructor, you should override the OnNavigatedTo and OnNavigatedFrom methods on your Page . This way you can remove timers when you navigate away from your page and set them up again when you come back.

protected override void OnNavigatedTo(NavigationEventArgs e)
{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM