OnDissapearing event does not execute in Xamarin.Forms (using FreshMVVM)

I am trying to show an animation when the current page is disappearing as well as when the next page is appearing, the OnAppearing event works fine, the problem is that OnDissapearing event in the current page does not execute, I think it is because I am using FreshMVVM and therefore I am calling the new page from the view model and not from the code behind of the page. I might be wrong, anyway, here is my code:

Current Page ViewModel:

public class LogInViewModel : FreshBasePageModel
    {
        public ICommand LogIn { get; set; }

        public InavigationService navigationService;

        public LogInViewModel(InavigationService _navService)
        {
            navigationService = _navService;

//This command calls a class where the page transition is made
            LogIn = new Command(() =>
            {
                navigationService.SwitchNavigationStacks(Enums.NavigationStacks.MainPage, this);
            });
        }
    }

NavigationService class:

class NavigationService : InavigationService
    {
        //Tabbed pages and its members are declarated here in order to enable navigation inside of it (it needs to be static)
        private static Page Products, Discover, Account, Settings;

        private static FreshTabbedNavigationContainer TabbedPageContainer = null;


        public void SwitchNavigationStacks(NavigationStacks navigation, FreshBasePageModel page)
        {
            switch (navigation)
            {
                case NavigationStacks.Login:
                    {
                        var thePage = FreshPageModelResolver.ResolvePageModel<LogInViewModel>();
                        var PageType = new FreshNavigationContainer(thePage, navigation.ToString());

                        page.CoreMethods.SwitchOutRootNavigation(navigation.ToString());

                        NavigationContainer = null;
                        TabbedPageContainer = null;
                        break;
                    }

//This is where the transition to the next page takes action on the SwitchOutRootNavigation event
                case NavigationStacks.MainPage:
                    {
                        try
                        {
                            if (TabbedPageContainer == null)
                            {
                                TabbedPageContainer = new FreshTabbedNavigationContainer(navigation.ToString());
                                Products = TabbedPageContainer.AddTab<HomeViewModel>(null, "IconHomeInverted.ico", null);
                                Discover = TabbedPageContainer.AddTab<HomeViewModel>(null, "IconMagnifyingGlassInverted.ico", null);
                                Account = TabbedPageContainer.AddTab<HomeViewModel>(null, "IconUserInverted.ico", null);
                                Settings = TabbedPageContainer.AddTab<HomeViewModel>(null, "IconSettingsInverted.ico", null);

                                #region UI
                                //Dissables swipe only in android because in IOS can not be done
                                TabbedPageContainer.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
                                TabbedPageContainer.BarTextColor = Color.FromHex("#FFFFFF");
                                #endregion

                                page.CoreMethods.SwitchOutRootNavigation(navigation.ToString());
                            }
                        }
                        catch
                        {
                        }
                        break;
                    }
            }
        }
    }
}

CurrentPage.xaml:

<Button
                x:Name="Button_LogIn"

                VerticalOptions="Start"
                HorizontalOptions="Start"
                Margin="25,45,25,5"
                HeightRequest="50"
                WidthRequest="400"
                BackgroundColor="WhiteSmoke"
                CornerRadius="25"
                Text="Log in"
                TextColor="#4847FF"
                FontAttributes="Bold"
                FontSize="20"

                Command="{Binding LogIn}"
            ></Button>

CurrentPage.xaml.cs:

public partial class LogInPage : ContentPage
    {
        public LogInPage()
        {
            InitializeComponent();

            if (Device.RuntimePlatform == Device.iOS)
            {
                usernameEntry.BorderWidth = 3;
                passwordEntry.BorderWidth = 3;
            }
        }
        protected override void OnDisappearing()
        {
//This event is never executed
            System.Threading.Thread.Sleep(6000);
        }
    }

I have tried to add a "Clicked" event on the button in my page which calls the OnExecuted event, but it executes at the same time as the command, so in the end, the onAppearing event in the other page executes before the OnDissapearing event of the current page and after this is executed, the OnAppearing event is executed 3 times in a row, it's chaotic.

I was thinking on creating just one event "Clicked" and to remove the command, so from the Clicked event I would call the OnDisappearing one, and then I would call from the page code behind (at the end of the OnDisappearing event) a method from the viewModel, but I don't know if this can be done following the MVVM principles or if there is a better way.

And that is all, if you need more information I will provide it to you as soon as I see your request, thank you all for your time and have a good day.

1 answer

  • answered 2020-03-31 19:30 Morse

    Have you tried exclipit event ?

    this.Disappearing += LogInPage_Disappearing;
    
    private void LogInPage_Disappearing(object sender, System.EventArgs e)
    {
    
    }
    

    Also dont use Thread.Sleep, use Task.Delay asynchronously instead i.e (await Task.Delay(8000)) I believe Task.Delay should hit during Debugging irrespective of exclipit event handling.