xamarin.forms - Xamarin Forms:轮播子页面未触发消失事件?
问题描述
我有一个有 9 个子页面的轮播页面,并且我在一些子页面上实现了跟踪功能。跟踪功能适用于视频、音频和内容。如果用户播放视频、音频或阅读内容,然后用户移动到下一个孩子或当时返回上一页,我将跟踪花费的时间。
我在OnDisappearing()
子页面上添加了跟踪服务。因此,当用户离开页面时,跟踪服务开始触发,并且在 android 平台上运行良好。当我在 ios 平台上测试它时,它OnDisappearing()
没有触发。
这是一个已知问题吗?我该如何解决这个问题?
解决方案
Event OnDisappearing在我这边在 iOS 和 Android 上都可以正常工作。如果您的项目仍然存在问题。您可以使用以下解决方法。
在xml中
<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;
}
}
}
}
推荐阅读
- python - 蟒蛇3。如何检查后缀是否在列表中的某些单词中?
- python - Discord 丰富的嵌入按钮
- angular - 具有 5000 个值的 Angular Material Form 自动完成
- c++ - 如何调试`InputPort::Eval(): required InputPort[0] is not connected`
- javascript - Gulp.watch 刷新我的浏览器一次,但之后不再刷新
- r - 循环 | 情节菜单
- prometheus - 普罗米修斯计数和总和指标超过 pushgateway 的重启
- oracle - Orcale 行为与左外连接或条件
- python - 检查帖子被调用的次数
- c - C 中奇怪的代码行为,同时使用注释代码的某个不相关部分会影响输出的指针