c# - IOS上的OnAppearing方法

标签 c# xamarin xamarin.forms xamarin.ios xamarin.android

当我的应用程序在后台并转到前台时,OnAppearing() 方法在 IOS 中不起作用,或者当手机锁定然后解锁并且应用程序在前台时,OnAppearing() 方法不会被调用,在 Android 上一切正常。 我在下面找到了这个指南,但仍然不起作用,我有最新版本的 xamarin 表单。

指南: https://kent-boogaart.com/blog/hacking-xamarin.forms-page.appearing-for-ios

谁能帮帮我?

最佳答案

如您所见,iOS 中的“生命周期”是不同的。一种有用的方法是使用应用程序生命周期并将它们绑定(bind)到 Page 事件(或命令,如果需要)。

在您的 Application 子类中添加几个公共(public) EventHandlers 并将它们绑定(bind)到 OnResume(如果需要,还绑定(bind)到 OnSleep)

public partial class App : Application
{
    public EventHandler OnResumeHandler;
    public EventHandler OnSleepHandler;

    public App()
    {
        InitializeComponent();
        MainPage = new MyPage();
    }

    protected override void OnSleep()
    {
        OnSleepHandler?.Invoke(null, new EventArgs());
    }

    protected override void OnResume()
    {
        OnResumeHandler?.Invoke(null, new EventArgs());
    }
}

现在在您的 ContentPage 子类中,添加一个处理程序来跟踪该页面何时从后台返回,类似于“OnAppearing after OnPause”处理程序...

void Handle_OnResumeHandler(object sender, EventArgs e)
{
    Console.WriteLine("OnPauseResumeWithPage");
}

protected override void OnAppearing()
{
    (App.Current as App).OnResumeHandler += Handle_OnResumeHandler;
    base.OnAppearing();
}

protected override void OnDisappearing()
{
    (App.Current as App).OnResumeHandler -= Handle_OnResumeHandler;
    base.OnDisappearing();
}

关于c# - IOS上的OnAppearing方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52186542/

相关文章:

android - 我对 spinner.setAdapter (Xamarin) 有错误

c# - Xamarin 转换器不适用于 IsEnabled 属性

c# - Xamarin iOS - MKPinAnnotationView.Image 不显示自定义图像

c# - 使用来自 Firebase 数据库表 Xamarin 的数据填充 Spinner

Xamarin Forms - 隐藏 MasterDetailPage 中的导航栏

c# - 系统参数异常 mvc .net

c# - 如何从线程中停止 Windows 服务应用程序?

C# 调试属性

c# - 如何在 C# 中仅从 DateTime 序列化 Xml Date

android - 列出我的 Android App 打开的所有文件