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?
I thing that you can use also "Loaded" event:
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"
>
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With