Sreejith Sree
Sreejith Sree

Reputation: 3681

Xamarin Forms: Carousel child pages not firing Disappearing event?

I have a Carousel page having 9 children and I have implemented a tracking feature on some of the child pages. The tracking feature is for video, audio and content. If a user plays a video, audio, or read content and then the user moves to the next child or going back to the previous page at that time I will track the spent time.

I have added the tracking service on the OnDisappearing() of child pages. So when a user leaves a page the tracking service starts triggering and it is working fine on the android platform. When I test it on the ios platform, the OnDisappearing() is not firing.

Is it a known issue? How can I fix this issue?

Upvotes: 0

Views: 316

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

The Event OnDisappearing works fine on my side both on iOS and Android . If the issue still exist on your project . You could use the following workaround .

in xaml

<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:local="clr-namespace:CarouselPageDemo"
             x:Class="CarouselPageDemo.MyCarouselPage"

              CurrentPageChanged="CarouselPage_CurrentPageChanged">

in CarouselPage.xaml.cs

 int CurrentIndex = 0;

//...

    private void CarouselPage_CurrentPageChanged(object sender, EventArgs e)
    {
        var index = this.Children.IndexOf(this.CurrentPage);

 

        var ind = CurrentIndex.ToString();

 

        if (index != CurrentIndex)
        {
            MessagingCenter.Send<Object>(this, ind);
        }

 

        if (index > -1)
        {
            CurrentIndex = index;
        }

 

    }

in child page

public Page1()
{
  InitializeComponent();

 

  MessagingCenter.Subscribe<Object>(this, "0", (org) =>
  {

 
     //   ... handle the logic when the page will disappearing

  });
}

Note : the 0 here will different in your child pages (0,1,2,...)

Update

    public partial class MainPage : CarouselPage
    {


        int LastIndex=0;

        public MainPage()
        {
            InitializeComponent();
            this.Children.Add(new Page1());
            this.Children.Add(new Page2());
            this.Children.Add(new Page3());
            CurrentPage = Children[0];

            MessagingCenter.Subscribe<App, string>((App)Xamarin.Forms.Application.Current, "child", (s, child) =>
            {
                CurrentPage = Children[Int32.Parse(child)];
            });
        }

        private void CarouselPage_CurrentPageChanged(object sender, EventArgs e)
        {
            var index = this.Children.IndexOf(this.CurrentPage);
            string title = this.CurrentPage.Title;
            Debug.WriteLine("title:>>" + title);
            if (!string.IsNullOrEmpty(title)&& LastIndex!=index)
            {
                MessagingCenter.Send<MainPage>(this, title);
            }

            if (index > -1)
            {
                LastIndex = index;
            }

        }
    }
}

Upvotes: 1

Related Questions