【问题标题】:Xamarin.Forms : Does anyone know what's the equivalent of WillEnterForeground and DidEnterBackground in Xamarin.Forms?Xamarin.Forms:有谁知道 Xamarin.Forms 中 WillEnterForeground 和 DidEnterBackground 的等价物是什么?
【发布时间】:2014-08-26 12:08:15
【问题描述】:

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

【问题讨论】:

    标签: mobile xamarin.ios xamarin xamarin.android xamarin.forms


    【解决方案1】:

    我认为 Xamarin.Forms 中还没有该功能。您可以安装 Xamarin.Forms.Labs(在 NuGet 上可用)并从 XFormsApplicationDelegate 继承您的应用委托。您可以查看 GitHub 源代码上的 sample app delegate

    public partial class AppDelegate : XFormsApplicationDelegate
    {
    

    然后您需要使用 DI 容器注册 IXFormsApp 接口:

        private void SetIoc()
        {
            var resolverContainer = new SimpleContainer();
    
            var app = new XFormsAppiOS();
            app.Init(this);
    
            resolverContainer.Register<IXFormsApp>(app);
    
            Resolver.SetResolver(resolverContainer.GetResolver());
        }
    

    在 DI 容器上注册应用程序后,您将通过解析器在共享/PCL 代码上使用它。您将订阅 Resumed 和 Suspended 事件。 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");
    

    【讨论】:

    • 谢谢 - 我错误地尝试使用 DependencyService 解析 IXFormsApp,它的工作机会几乎为零。
    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多