In WP7 and WP8 I just needed to clear the backstack in a page, then press Back button and the app is closed. In WP8.1 I do Frame.BackStack.Clear(), press Back and the app just minimizes.. How to kill it with Back button?
You can add, in your main page definition:
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Then
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (!e.Handled && Frame.CurrentSourcePageType.FullName == "YourApp.MainPage")
Application.Current.Exit();
}
Warning: As others said, you should not use this and let the system handle the app closure. For example, if you use the Application Insights, I found that they are not sent to Azure when in Release mode
I think the above has been depreceated. Exit is now an event.
Try
Application.Current.Terminate();
you can simply create a button by using XAML and then add this code into your Main page xaml.cs
Application.Current.Exit();
MSDN recommends to not close apps in Windows 8.1:
We recommend that apps not close themselves programmatically unless
absolutely necessary. For example, if an app detects a memory leak, it
can close itself to ensure the security of the user's personal data.
When you close an app programmatically, the system treats this as an
app crash.
https://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx#close
Related
We have a Xamarin UWP app that needs to be logged out whenever a user minimizes or clicks away from the window.
In my App.xaml.cs I have registered an event handler for the Suspending event. I then put our logout code in this event handler like so:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.GetDeferral();
AppBackgrounded();
deferral.Complete();
}
This AppBackgrounded() method looks like this:
void AppBackgrounded()
{
if (!_isInBackgroundMode)
{
_isInBackgroundMode = true;
if (UserSetPin)
{
PinPage passcodePin = new PinPage();
Navigation.PushModalAsync(New NavigationPage(passcodePin), false);
}
else
{
App.Logout(null, true, true);
}
// clears the pasteboard so data can't be copied from this app into other apps
Clipboard.Clear();
}
}
We also have a AppLeavingBackground method that we use to restore the app when the user returns, but the app does not crash when returning. It only crashes when running the OnSuspended method.
This crash only occurs when
The App is built for release and
The device is in tablet mode
When in tablet mode, if you press the Task View button and navigate to another application the UWP app freezes trying to run through this code. If you try to return to the app, it will immediately exit.
I have tried to make the navigation to the other pages async and the app will then crash even when its not in tablet mode. I have also tried to put this logic in AppEnteredBackground and it still occurs.
This is hard to debug since it only occurs in release mode. Any ideas?
In my case it was the Clipboard.Clear() function that was crashing the application. For those encountering similar issues, checkout the other answers as they all provide great points.
As a side note, I also found that using async code in these events was crashing my application. I'm not sure why since I was using the deferral, but since it's working i'm not going to pursue it further.
I cannot say that I have read exactly the documentation that says that navigating the pages in suspending will result in a crash, but it is clear that this is the wrong place to do it.
The suspending is not used to prepare your app to be opened again, the whole reason for this lifecycle event is that you need to prepare your app for not being open again, which means saving some data that may eventually get lost. Preparing your app to be opened again is done in resuming.
I am developing an app based on a mobile website which runs inside a Windows Runtime WebView for Windows Phone 8.1. Currently, I am looking for a way to catch link clicks in the app's webview which start with a certain pattern. In the Windows Phone Silverlight version, the following was possible:
In my webview I display links like this
Do something in the app
Do something different in the app
In the app, I catch a click on such a link with the following code
MyWebView.Navigating += HandleOwnStuff;
void HandleOwnStuff(object sender, NavigatingEventArgs e) {
String url = e.Uri.ToString();
if(url.StartsWith("ownstuff:")) {
// check which function has been called and do something in the app, e.g. open the camera
}
}
Unfortunately, this approach is not possible in a WinRT app any more. Whenever I click such a link, the launcher opens with the message "Search for an app in the store".
The "NavigationCompleted" handler of the WebView is not called - this link click is catched before any handler of the WebView is called.
An approach which would work is using the UnsupportedUriSchemeIdentified handler, which - unfortunately - is only available starting from Windows 10 in UWP.
Another solution would be to completely rewrite my website so that the ScriptNotify handler can be invoked - which would be way too much work, since the website now also runs in normal browsers, Android and iOS devices - so this approach is definitely not worth the trouble.
Does anyone know a solution? Thanks.
I have talked about this with MSFT for awhile. Your right, with win10 that's why that event was added. The best way to workaround it is to change the website.
In my Universal app, (Windows 8.1 Windows Phone8.1), I want to know the reason why my app is suspended like due to a launch of a launcher(Mail, File picker etc) or user pressed the Windows button. Is there a way to determine this?
Unfortunately you can't know what caused the app's suspension. Unlike the LaunchActivatedEventArgs in the OnLaunched method for example, that contain the Kind property the SuspendingEventArgs in the OnSuspending method don't provide any such information.
However, there are only so many reasons that an app can be suspended. Like you said (in windows phone only) it can be suspended due to a launch of a launcher or a protocol and by the windows button.
The windows button is the only suspension way of which you have no control. For all the others you can trick the system and for example set a static global variable that you update when you launch an operation that would suspend your app and check it in OnSuspending
In my app (WP 8.1 RT) I use the DataTransferManager to share content. After launching the sharing UI, the app returns back to the page(2nd page of the app). And then, when I press the back button it's suppose to go back to the previous page(MainPage) but instead, its exiting the app. Back buttonn is working properly before launching the sharing UI. And this doesn't happen when debugging.
This is the order of the events happening.
Page1(main) -> Page2 -> (Calling DataTransferManager)SharingUI -> Backto Page2. Then back button exits the app.
What could be the cause?
I can't reproduce this issue. How do you handler back button pressing? Try using NavigationHelper instead of using custom BackPressed handler. I hope it will help you.
Im working on a Windows Phone 8 App that would fill out a particular webpage's form textboxes upon navigating to the page.
The code Im looking for would be something similar to this:
private void Button_Click(object sender, RoutedEventArgs e)
{
MiniBrowser.Source = new Uri(site, UriKind.Absolute);
MiniBrowser.Document.GetElementById("ElementIdHhere").SetAttribute("value", TextBox1.Text);
MiniBrowser.Document.GetElementById("ElementIdHhere").SetAttribute("value", TextBox2.Text);
}
However, Windows Phone platform doesnt seem to have support for WebBrowser.Document.GetElementById. Is there a way to do this in Windows Phone enviroment?
The only way to interact with the loaded content of the WebBrowser control is via JavaScript. The internals of the page are not made available to managed code.
This solution would also be dependent upon the HTML being loaded not preventing you from executing an eval method.
The other thing to consider that is not covered by the code in your question is that this will not be possible until the contents of the page have finished loading and this is not something you can see from managed code. You'll probably have to wait a few seconds to be sure it has finished rendering and can successfully process your JavaScript commands.