none
While my System.Timers.Timer(); code is running, when I am navigating to any page, it is displaying as a blank page with no content. (Xamarin Forms) RRS feed

  • Question

  • Timer Code on page load:

    DateTime endTime = new DateTime(2019, 08, 28, 14, 00, 0);
    public void StartCountDownTimer()
    {
        try
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += t_Tick;
            TimeSpan ts = endTime - DateTime.Now;        
            lblCountDown.Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
            timer.Start();
        }
        catch (Exception ex)
        {
            string Error = ex.Message;
        }
    }
    
    
    System.Timers.Timer timer;
    void t_Tick(object sender, EventArgs e)
    {
            TimeSpan ts = endTime - DateTime.Now;
            string NewTimer = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
            //txtCountDown.Text = NewTimer;
            lblCountDown.Text = NewTimer;
            if ((ts.TotalMilliseconds < 0) || (ts.TotalMilliseconds < 1000))
            {
                timer.Stop();
                lblCountDown.Text = "The day has arrived";
            }
       
    }

    Navigate Code using a button click on the same page:

    private void ClickAboutTab(object sender, EventArgs e){await Navigation.PushAsync(new Page());}





    Monday, August 26, 2019 1:40 PM

Answers

  • Hi 

    Thanks for the feedback.

    As you want to change the UI stuff in the timer you need to make sure these code is triggered on the main thread. So modify it like:

    void t_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = endTime - DateTime.Now;
        string NewTimer = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
        //txtCountDown.Text = NewTimer;
        Device.BeginInvokeOnMainThread(() =>
        {
            lblCountDown.Text = NewTimer;
        });
                
        if ((ts.TotalMilliseconds < 0) || (ts.TotalMilliseconds < 1000))
        {
            timer.Stop();
            lblCountDown.Text = "The day has arrived";
        }
    
    }

    This will help you successfully navigate to a new page when timer is beginning. However, I recommend you to stop timer before pushing:


    //timer.Stop();

    Navigation.PushAsync(new Page());

    The original page will be overlapped after navigating so the t_Tick method makes no sense. I recommend you to stop it and restart it when the page becomes to display at the top stack.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 27, 2019 8:46 AM
    Moderator

All replies

  • Does it work if you execute ‘timer.SynchronizingObject = this’ before starting the timer?

    Monday, August 26, 2019 5:40 PM
  • Hi Goodman Ngwenya,

    Thank you for posting here.

    I’ve noticed that you used

    private void ClickAboutTab(object sender, EventArgs e){await Navigation.PushAsync(new Page());}

    to navigate to a new page. However, this page hasn’t been configured. You only used a blank page so that nothing displays.

    You’d better create your own ContentPage then navigate to it in your button’s click event.

    Refer to this documentation to customize the content page.

    Best   Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 27, 2019 3:22 AM
    Moderator
  • HI Jack, Please note that this happens with any page, I just added that code as an example to explain my issue. I do have code with content and when it navigates to that pages, it appears as blank but as soon as I comment the timer code out, then it displays my content on navigation.
    Tuesday, August 27, 2019 7:57 AM
  • This causes the timer to not work
    Tuesday, August 27, 2019 8:07 AM
  • Hi 

    Thanks for the feedback.

    As you want to change the UI stuff in the timer you need to make sure these code is triggered on the main thread. So modify it like:

    void t_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = endTime - DateTime.Now;
        string NewTimer = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
        //txtCountDown.Text = NewTimer;
        Device.BeginInvokeOnMainThread(() =>
        {
            lblCountDown.Text = NewTimer;
        });
                
        if ((ts.TotalMilliseconds < 0) || (ts.TotalMilliseconds < 1000))
        {
            timer.Stop();
            lblCountDown.Text = "The day has arrived";
        }
    
    }

    This will help you successfully navigate to a new page when timer is beginning. However, I recommend you to stop timer before pushing:


    //timer.Stop();

    Navigation.PushAsync(new Page());

    The original page will be overlapped after navigating so the t_Tick method makes no sense. I recommend you to stop it and restart it when the page becomes to display at the top stack.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, August 27, 2019 8:46 AM
    Moderator