Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAUI Blazor App - Run Code On Any Page Load?

Is it possible to run a piece of code on every time a page is loaded whether its being navigated to or any other scenario it may be?

Something like overriding OnNavigate method?

like image 356
LearningEveryDay2.0 Avatar asked Oct 21 '25 04:10

LearningEveryDay2.0


2 Answers

I thing that you can use also "Loaded" event:

  1. Add the line Loaded="Page_Loaded" to your ContentPage XAML, like this:

    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         ...
         Loaded="Page_Loaded" 
    >
    
  2. Add "Page_Loaded" event code to your page code-behind (.cs) file:

    private async void Page_Loaded(object sender, EventArgs e)
    {
     <Your code here>            
    }
    

I hope this helps.

like image 65
rentoulis Avatar answered Oct 26 '25 17:10

rentoulis


You can override OnAppearing method in the .xaml.cs code of the page, and add a piece of code specified to OnAppearing. OnAppearing means that when the page appears, it will be called.


public partial class Page : ContentPage
{
    public Page()
    {
        InitializeComponent();
    }

   protected override void OnAppearing()
    {
        base.OnAppearing();
        //a piece of code specified
    }

}

like image 23
Jianwei Sun - MSFT Avatar answered Oct 26 '25 18:10

Jianwei Sun - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!