Maui - Using Timer in multi main page navigations

Dani_S 4,461 Reputation points
2024-03-03T10:46:21.53+00:00

Hi,

Here is my code:

Did I used in correcy way ?

Thanks in advance,

  private IDispatcherTimer _indicatorTimer;


public MainPage(string navigateTo)
  {


  InitializeComponent();

  currentContentViewHolder.Children.Clear();

  if (navigateTo == "InProcessView")

  {

      currentContentViewHolder.Children.Add(new InProcessView());

      inProcessButton.HasSelect = true;

  }

  else if (navigateTo == "DownloadView")

  {

      currentContentViewHolder.Children.Add(new DownloadView());

      downloadButton.HasSelect = true;

  }

  else if (navigateTo == "CopyView")

  {

      currentContentViewHolder.Children.Add(new CopyView());

      copyButton.HasSelect = true;

  }


  

  this.Loaded += MainWindow_Loaded;

  this.Unloaded += MainWindow_unloaded;
 }

  private void MainWindow_unloaded(object sender, EventArgs e)

  {


  if (_indicatorTimer != null)

  {

      _indicatorTimer.Stop();

  }
  }

  private void MainWindow_Loaded(object sender, EventArgs e)

  {

  RunIndicatorInTimer();
  }

  public MainPage()

{
InitializeComponent();

    


  currentContentViewHolder.Children.Clear();

  currentContentViewHolder.Children.Add(new UploadView());

 uploadButton.HasSelect = true;


 

  this.Loaded += MainWindow_Loaded;

  this.Unloaded += MainWindow_unloaded;
  }

 private void RunIndicatorInTimer()

 {


 SetIndicatorStatus();

 if (_indicatorTimer == null)

 {

     var dispatcher = this.Dispatcher;

     _indicatorTimer = dispatcher.CreateTimer();

     _indicatorTimer.Interval = TimeSpan.FromSeconds(60);

     _indicatorTimer.Tick += (s, e) =>

     {

         SetIndicatorStatus();

     };

     _indicatorTimer.IsRepeating = true;

     _indicatorTimer.Start();

 }
 }

I have docklayout with header, menu on left and footer.

The currentContentViewHolder is the last child, this is a grid:

<Grid x:Name="currentContentViewHolder" HorizontalOptions="Fill" VerticalOptions="Fill" Margin="0,0,5,0">

</Grid>

1.Is it ok to change different View over time with paramerter(first cto'r )and without the second cto'r?

2.What about the timer in this case:

doe loaded and unloaded are correct events ? does the implemntation of timer is correct? the handler of the timer -SetIndicatorStatus method should use invoke ? if yes how ? do i need to in unloaded event to close the timer it happened when navigating.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,105 questions
{count} votes

Accepted answer
  1. Anonymous
    2024-03-04T07:45:02.62+00:00

    Hello,

    =============Update==============

    Did you enable the XAML hot reload in your VS?

    If so, XAML Hot reload types are present in the managed heap. This may cause objected to be retained that would be otherwise garbage collected

    Please disable the XAML hot reload(Tools->Options->debugging->XAML Hot reload-> unselect Enable XAML Hot reload), then debug it.

    I update to the Microsoft Visual Studio Community 2022 Version 17.9.2 and test navigation. After several times navigation. The memory will not keep increasing and will maintain a stable value.

    1.Is it ok to change different View over time with paramerter(first cto'r )and without the second cto'r?

    Firstly, two construction methods, only one of them will be executed every time the page is loaded. If you have two ways to open the MainPage(One is needing attribute, another is not containing attribute, you can keep both of them.

    .What about the timer in this case:

    doe loaded and unloaded are correct events ?

    Do you want to load the data or start/stop timer when page appearing/disappearing? I think OnAppearing method and OnDisappearing method is better,

    does the implemntation of timer is correct?

    Yes

    the handler of the timer -SetIndicatorStatus method should use invoke ? if yes how ?

    What does the SetIndicatorStatus method do? I cannot get the detailed code. If you want to change the UI, you need to use MainThread to wrap the change the UI code.

    MainThread.BeginInvokeOnMainThread(() => { // hange the UI code. });

    do i need to in unloaded event to close the timer it happened when navigating.

    Yes

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.