问题描述
我有一个有9个子页面的“轮播”页面,并且在某些子页面上实现了跟踪功能。跟踪功能适用于视频,音频和内容。如果用户播放视频,音频或阅读内容,然后用户移至下一个孩子或当时返回上一页,则我将跟踪所花费的时间。
我已在Ondisappearing()
子页面上添加了跟踪服务。因此,当用户离开页面时,跟踪服务将开始触发,并且在android平台上可以正常工作。当我在ios平台上对其进行测试时,Ondisappearing()
没有启动。
解决方法
事件 OnDisappearing 对我来说在iOS和Android上均正常运行。如果问题仍然存在于您的项目中。您可以使用以下解决方法。
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">
在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;
}
}
在子页面中
public Page1()
{
InitializeComponent();
MessagingCenter.Subscribe<Object>(this,"0",(org) =>
{
// ... handle the logic when the page will disappearing
});
}
注意::此处的 0 在您的子页面中会有所不同(0、1,2,...)
更新
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;
}
}
}
}