我有一个旋转木马页面,有9个孩子,我已经实现了一个跟踪功能的一些子页面。跟踪功能用于视频、音频和内容。如果用户播放视频、音频或读取内容,然后用户移动到下一个子页面或返回到前一个页面,那么我将跟踪花费的时间。
我在子页面的OnDisappearing()
上添加了跟踪服务。因此,当用户离开页面时,跟踪服务开始触发,并且在android平台上运行良好。当我在ios平台上测试它时,OnDisappearing()
没有启动。
这是一个众所周知的问题吗?我怎样才能解决这个问题?
发布于 2020-09-08 14:06:14
在我这一边,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,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;
}
}
}
}
https://stackoverflow.com/questions/63790331
复制相似问题