gpt4 book ai didi

mobile - Xamarin.Forms : Does anyone know what's the equivalent of WillEnterForeground and DidEnterBackground in Xamarin. 表单?

转载 作者:行者123 更新时间:2023-12-02 11:46:53 25 4
gpt4 key购买 nike

我正在将在 Xamarin.iOS 中编写的应用程序重写为 Xamarin.Forms,并且我曾经有一些代码要在 DidEnterBackground 和 WillEnterForeground 上执行。现在我在 Xamarin.Forms 中找不到等效的方法。我在我的应用程序类中尝试过 mainPage.Appearing 和 mainPage.Disappearing 但它们似乎与我想要实现的目标不同。有人吗?

最佳答案

我不相信 Xamarin.Forms 中有该功能。您可以安装 Xamarin.Forms.Labs(在 NuGet 上提供)并从 XFormsApplicationDelegate 继承您的应用程序委托(delegate)。你可以看看sample app delegate在 GitHub 来源上。

public partial class AppDelegate : XFormsApplicationDelegate
{

然后您需要向 DI 容器注册 IXFormsApp 接口(interface):

    private void SetIoc()
{
var resolverContainer = new SimpleContainer();

var app = new XFormsAppiOS();
app.Init(this);

resolverContainer.Register<IXFormsApp>(app);

Resolver.SetResolver(resolverContainer.GetResolver());
}

应用程序在 DI 容器上注册后,您可以通过解析器在共享/PCL 代码上使用它。您将订阅“恢复”和“暂停”事件。 Sample here.

        var app = Resolver.Resolve<IXFormsApp>();
if (app == null)
{
return;
}

app.Closing += (o, e) => Debug.WriteLine("Application Closing");
app.Error += (o, e) => Debug.WriteLine("Application Error");
app.Initialize += (o, e) => Debug.WriteLine("Application Initialized");
app.Resumed += (o, e) => Debug.WriteLine("Application Resumed");
app.Rotation += (o, e) => Debug.WriteLine("Application Rotated");
app.Startup += (o, e) => Debug.WriteLine("Application Startup");
app.Suspended += (o, e) => Debug.WriteLine("Application Suspended");

关于mobile - Xamarin.Forms : Does anyone know what's the equivalent of WillEnterForeground and DidEnterBackground in Xamarin. 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com