Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/Xamarin: Appearing/Disappearing events don't fire

UPDATE: I've looked into this issue further and discovered this is a Xamarin.Forms issue.

To prove the issue I've created the following simple Xamarin.Forms app, from a basic PLC template.

I added a new page with simple handlers for Appearing and Disappearing events, as so.

using System;
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EventPage : ContentPage
    {
        public EventPage()
        {
            InitializeComponent();
            Appearing += EventPage_OnAppearing;
            Disappearing += EventPage_OnDisappearing;
        }

        private void EventPage_OnAppearing(object sender, EventArgs e)
        {
            Debug.WriteLine("Appeared");
        }

        private void EventPage_OnDisappearing(object sender, EventArgs e)
        {
            Debug.WriteLine("Disappeared");
        }
    }
}

I then added two buttons to the main page which navigate to the above page in two different ways:

using System;
using Xamarin.Forms;

namespace TestApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void WorkingButton_OnClicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new EventPage());
        }

        private void NonWorkingButton_OnClicked(object sender, EventArgs e)
        {
            Application.Current.MainPage = new EventPage();
        }
    }
}

Observations are the same as before, with PushAsync the behaviour is as expected, but setting the root page means the events don't fire.

This came up in my real project when I reset the stack using this line await _navigationService.NavigateAsync($"http://app.com/NavigationPage/HomePage/AlertPage"); so the issue doesn't seem to be isolated to just the root page.

I've come across this issue in my current project which is using Xamarin Forms v2.3.4.192-pre2 with Prism 6.3.0-pre2 (Unity) and in the following test app using Xamarin Forms 2.3.3.193 with Prism 6.3.0 (Dryloc). I'm running on a LG G5, Android 7.0.

I'm not sure if this is a Prism issue or a Xamarin Forms issue as I'm not quite sure how to rewrite this as pure Forms.

To prove the issue I made the following test app.

EventPage has no changes from the basic Prism ContentPage template other than EventPage.xaml.cs, where I've assigned basic handlers to the Appearing and Disappearing Events:

using System;
using System.Diagnostics;
using Xamarin.Forms;

namespace PrismErrorDemo.Views
{
    public partial class EventPage : ContentPage
    {
        public EventPage()
        {
            InitializeComponent();
            Appearing += EventPage_OnAppearing;
            Disappearing += EventPage_OnDisappearing;
        }

        private void EventPage_OnAppearing(object sender, EventArgs e)
        {
            Debug.WriteLine("Appeared");
        }

        private void EventPage_OnDisappearing(object sender, EventArgs e)
        {
            Debug.WriteLine("Disappeared");
        }
    }
}

My MainPage has two simple buttons which navigate to my EventPage, one relatively and one absolutely.

using System;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;

namespace PrismErrorDemo.ViewModels
{
    public class MainPageViewModel : BindableBase
    {
        private readonly INavigationService _navigationService;

        public DelegateCommand WorkingCommand { get; set; }
        public DelegateCommand NonWorkingCommand { get; set; }

        public MainPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            WorkingCommand = new DelegateCommand(WorkingCommandAction);

            NonWorkingCommand = new DelegateCommand(NonWorkingCommandAction);
        }

        private async void WorkingCommandAction()
        {
            await _navigationService.NavigateAsync("EventPage");
        }

        private async void NonWorkingCommandAction()
        {
            await _navigationService.NavigateAsync("http://app.com/EventPage");
        }
    }
}

When I navigate using await _navigationService.NavigateAsync("EventPage"); things behave exactly as they should. Both event handlers are called when they are supposed to be.

However, when I navigate using await navigationService.NavigateAsync("http://app.com/EventPage"); I get an Appearing event called as I navigate to the page, and using the back button to close the app will result in a Disappearing event being called. All other times closing and reopening the app fails to call either event.

I couldn't find any reference to this on the Github issues page and want to make sure I'm not missing anything obvious before posting this there.

like image 999
Daniel Reuben Watkins Avatar asked Apr 05 '17 10:04

Daniel Reuben Watkins


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


1 Answers

use following override method instead of custom

OnAppearing()

protected override void OnAppearing()
{
    base.OnAppearing();
}

OnDisappearing()

protected override void OnDisappearing()
{
     base.OnDisappearing();
}
like image 175
ajaysinh rajput Avatar answered Sep 30 '22 14:09

ajaysinh rajput