【问题标题】:navigator.app.exitApp() is not workingnavigator.app.exitApp() 不工作
【发布时间】:2016-08-24 05:23:06
【问题描述】:

我正在开发 Windows phone 8 PhoneGap 应用程序。使用 navigator.app.exitApp() 我正在从 Windows phone 7 的主屏幕退出应用程序。但是当我在 Windows phone 8 中尝试相同的操作时,我收到错误 Unable to get property 'exitApp' of undefined or null reference。我想知道为什么它在 Windows phone 8 中未定义,而不是在 Window phone 7 PhoneGap 应用程序中。另外,我想知道,有什么方法可以在 Windows phone 8 PhoneGap 应用程序中以编程方式退出该应用程序?

【问题讨论】:

    标签: javascript cordova


    【解决方案1】:

    您可以创建一个简单的插件。将文件 ExitApp.css 添加到您的平台/wp8/Plugins 文件夹:

    using System.Windows;
    
    namespace WPCordovaClassLib.Cordova.Commands
    {
      class ExitApp : BaseCommand
      {
        public void execute(string options)
        {
            Application.Current.Terminate();                        
        }
      }
    }
    

    编辑您的平台/wp8/config.xml 并添加到小部件标签:

    <feature name="ExitApp">
      <param name="wp-package" value="ExitApp" />
    </feature>`
    

    然后你的javascript调用:

    cordova.exec(null, null, "ExitApp", "execute", []);
    

    当用户点击主页中的后退按钮时,您可以将它与后退按钮事件结合使用来关闭应用程序:

    function goBack(e){
      if(isInMyMainPage()) cordova.exec(null, null, "ExitApp", "execute", []);
    }
    document.addEventListener("backbutton", goBack, false)
    

    【讨论】:

    • 为什么不赞成这个答案?我在生产中使用它。
    • 谢谢。它正在工作。一个更正:在 config.xml 添加 到插件标签
    【解决方案2】:

    我为 Windows Phone 8.1 开发小型应用程序,下面的代码适用于我:

    window.close();
    

    【讨论】:

      【解决方案3】:

      Navigator.app.exit() 不会退出应用程序,它会使应用程序崩溃。

      在 Windows Phone 8 中,它被处理,所以它只会抛出一个异常。

      您必须在 CordovaView.xaml.cs 的 page_BackKeyPress 事件中编写以下代码

      Application.Current.Terminate();
      

      它会在按下硬件后退按钮时退出您的应用程序。

      【讨论】:

      • Navigator.app.exit() 未在 Cordova 3+ 上的 WP8 中定义
      【解决方案4】:

      此处有类似问题:How to exit an application in window phone 8 with phonegap 2.3 包含一个不需要任何本机黑客攻击的修复程序。

      【讨论】:

        【解决方案5】:

        在 3.6.3 版中,navigator.app.exitApp() 可以工作。

        这是在 CordovaView.cs 中调用它的地方

        void CordovaBrowser_ScriptNotify(object sender, NotifyEventArgs e)
        {
            string commandStr = e.Value;
        
            string commandName = commandStr.Split('/').FirstOrDefault();
        
            if (browserDecorators.ContainsKey(commandName))
            {
                browserDecorators[commandName].HandleCommand(commandStr);
                return;
            }
        
            CordovaCommandCall commandCallParams = CordovaCommandCall.Parse(commandStr);
        
            if (commandCallParams == null)
            {
                // ERROR
                Debug.WriteLine("ScriptNotify :: " + commandStr);
            }
            else if (commandCallParams.Service == "CoreEvents")
            {
                switch (commandCallParams.Action.ToLower())
                {
                    case "overridebackbutton":
                        string arg0 = JsonHelper.Deserialize<string[]>(commandCallParams.Args)[0];
                        this.OverrideBackButton = (arg0 != null && arg0.Length > 0 && arg0.ToLower() == "true");
                        break;
                    case "__exitapp":
                        Debug.WriteLine("Received exitApp command from javascript, app will now exit.");
                        CordovaBrowser.InvokeScript("eval", new string[] { "cordova.fireDocumentEvent('pause');" });
                        CordovaBrowser.InvokeScript("eval", new string[] { "setTimeout(function(){ cordova.fireDocumentEvent('exit'); cordova.exec(null,null,'CoreEvents','__finalexit',[]); },0);" });
                        break;
                    case "__finalexit":
                        IsExiting = true;
                        // hide the browser to prevent white flashes, since about:blank seems to always be white
                        CordovaBrowser.Opacity = 0d; 
                        CordovaBrowser.Navigate(new Uri("about:blank", UriKind.Absolute));
                        break;
                }
            }
            else
            {
                if (configHandler.IsPluginAllowed(commandCallParams.Service))
                {
                    commandCallParams.Namespace = configHandler.GetNamespaceForCommand(commandCallParams.Service);
                    nativeExecution.ProcessCommand(commandCallParams);
                }
                else
                {
                    Debug.WriteLine("Error::Plugin not allowed in config.xml. " + commandCallParams.Service);
                }
            }
        }
        

        【讨论】:

          【解决方案6】:

          navigator.app.exitApp();自 3.4.0 起已在 Apache Cordova WP8 项目中可用

          <div onclick="navigator.app.exitApp()">Exodus</div>
          

          【讨论】:

            【解决方案7】:

            这里有类似的问题:https://groups.google.com/forum/#!msg/phonegap/9v2kOwXj6sQ/O8SVpd-qjicJ

            但它基本上说不应以编程方式退出 windows phone 8 应用程序。

            【讨论】:

            • 这不正确,你可以做一个简单的插件,看看我的回答
            猜你喜欢
            • 1970-01-01
            • 2014-05-15
            • 1970-01-01
            • 1970-01-01
            • 2021-01-10
            • 1970-01-01
            • 2019-01-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多