0

I need to just show a message (Under certain circumstance) when I'm leaving a screen. Found that there's a method called OnDisappearing() that is called when the form is being unloaded (Or also being overlaped by a new one).

What I found: https://forums.xamarin.com/discussion/89563/intercept-page-leaving-event https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.page.ondisappearing?view=xamarin-forms

Issue is that if I just copy the code as it is I get an error cause of the override (no suitable method found to override) that won't let me leave the code as is:

enter image description here

*Same happens with OnBackButtonPressed()

Modified it and just left it without the override and it just won't be called by any mean..

protected void OnDisappearing()
{
    Exiting();
}

private async void Exiting()
{
    System.Threading.Tasks.Task tmpShouldExit = Application.Current.MainPage.DisplayAlert("Hello", "Hi", "OK");
}

Is something I'm missing? Is there any other method I can use?

Thanks

Jorge Lizaso
  • 199
  • 1
  • 3
  • 15

1 Answers1

0

As Jason made me notice.

I was placing this in the ViewModel and it has to be in the code of the View cause you're overriding a Method of the Page.

Then if you want to access a method of the ViewModel from the view you can create a BindingContext to do so:

using MyProject.PageModels;
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace MyProject.Pages
{
    public partial class MyViewPage : ContentPage
    {
        public MyViewPage()
        {
            InitializeComponent();
            NavigationPage.SetBackButtonTitle(this, string.Empty);
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();

            var pageViewModel = (MyViewModel)this.BindingContext;

            if(pageViewModel.CertainConditionShowAlert())
            {
                 System.Threading.Tasks.Task tmpShouldExit = Application.Current.MainPage.DisplayAlert("Hi", "Hello", "OK");
            }

        }
    }

}
Jorge Lizaso
  • 199
  • 1
  • 3
  • 15