navigator.app.exitApp() is not working

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I am developing Windows phone 8 PhoneGap app. Using navigator.app.exitApp() I am quiting the app from home screen in Windows phone 7. But when I tried the same in Windows phone 8, I am getting the error Unable to get property 'exitApp' of undefined or null reference. I would like to know why it is undefined in Windows phone 8 and not in Window phone 7 PhoneGap app. Also, I would like to know, is there any way to quit the app programmatically in Windows phone 8 PhoneGap app?.

回答1:

You can create a simple plugin. Add file ExitApp.css to your platforms/wp8/Plugins folder with:

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

edit your platforms/wp8/config.xml and add to the widget tag:

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

then from you javascript call:

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

You can use it in combination with backbutton event to close the app when the user clicks on backbutton in the main page:

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


回答2:

I develop small application for Windows Phone 8.1 and the code below works for me:

window.close(); 


回答3:

Navigator.app.exit() will not exit the application it will crash the application.

In Windows Phone 8, it is handled so it will just throw an exception.

You will have to write the below code in page_BackKeyPress event in CordovaView.xaml.cs

Application.Current.Terminate(); 

It will exit your application on pressing hardware backbutton.



回答4:

Similar question here: How to exit an application in window phone 8 with phonegap 2.3 includes a fix that doesn't require any native hacking.



回答5:

In version 3.6.3, navigator.app.exitApp() does work.

Here is where it is called in 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(); has been available in Apache Cordova WP8 projects since 3.4.0

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


回答7:

Similar question here: https://groups.google.com/forum/#!msg/phonegap/9v2kOwXj6sQ/O8SVpd-qjicJ

But it basically says that windows phone 8 apps shouldn't be programatically exited.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!