Windows 10 mobile UWP - slow back button - c#

I have simply application written in c#, with sqlite database. I realized that it's not working fast on my phone. I'm pretty sure that problem is linked with functionality of pressed bulit-in back button. When I repeat several time this process:
Open new page -> return to previous page by back button, the application starting slows down.
When I added my own back button only to test, everything works fine.
I base mostly on this article:
http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps

Open new page -> return to previous page by back button, the application starting slows down.
After looking into your project, I found out the problem: You are registering SystemNavigationManager.GetForCurrentView().BackRequested +=OnBackRequested on every page. SystemNavigationManager.GetForCurrentView().BackRequested is an application scope event. It won't dispose the eventhandler when you are navigating between pages. You only need to register it once in your whole application.
So, to fix the problem, you can comment out all the BackRequested event registration of your pages's code-behind and keep only the one in your App.xaml.cs.
For example: in ProductsPage.xaml.cs comment out or delete following lines:
//SystemNavigationManager.GetForCurrentView().BackRequested += (s, e) =>
//{
// // TODO: Go back to the previous page
// Frame.Navigate(typeof(main1));
//};

If your back is Phone hard key you may handle the event.
The link is say the pc and add the back button and you should
SystemNavigationManager.GetForCurrentView().BackRequested +=OnBackRequested;
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame?.CanGoBack==true)
{
e.Handled = true;
rootFrame.GoBack();
}
else
{
Application.Current.Exit();
}
}
}
http://edi.wang/post/2016/2/1/windows-10-uwp-back-button-tricks
http://blog.csdn.net/lindexi_gd/article/details/50618029

Related

How do I capture mouse "back" button and cause it to do something else?

I have a Windows 8.1 Universal App I am working on and all of the pages are "inside" a root Frame object.
The landing page is a Home page and the user can select from 5 different pages to go to. When the user goes to one of those pages, the page has a "home" button on it to take them back to the Home page.
I recently discovered that if a mouse has a back button on it, it will go back to the last page visited. This method of going back to the Home page bypasses some logic that is necessary for the app.
Is it possible to "disable" any action of those forward and back buttons on the mouse? If it is not possible, can I capture that event and redirect it to the method that has the necessary logic?
Thanks,
Zach
I think you are looking for the concept of navigation service.
And this MSDN Article should help you to manage your backward navigation.
VK_XBUTTON1 and VK_XBUTTON2.
Those are the constants for the additional mouse buttons, that are often assigned to forward and backward navigation.
If you want to handle the X button messages immediately, they are posted to your application using WM_XBUTTONDOWN and WM_XBUTTONUP.
Check here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731.aspx
Edit: I just noticed that this question was about a "Universal app", this example is for WinForms but it may be helpful for finding the answer for Universal or UWP apps...
Assuming you already have two event handlers on buttons to navigate backwards (void NavigateBack(object sender, EventArgs e)) and forwards (void NavigateForward(object sender, EventArgs e))
First add this snippet to your Form code:
private void HandlePreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.XButton1:
NavigateBack(sender, e); // call the back button event handler
break;
case Keys.XButton2:
NavigateForward(sender, e); // call the forward button event handler
break;
}
}
private void HandleMouseDown(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.XButton1:
NavigateBack(sender, e); // call the back button event handler
break;
case MouseButtons.XButton2:
NavigateForward(sender, e); // call the forward button event handler
break;
}
}
Then go to the designer view, go through all the major controls on your form and hookup the PreviewKeyDown and MouseDown events to the corresponding methods.
A better (future proof) way would be to write code to recursively hookup the events like this:
private void HookupNavigationButtons(Control ctrl)
{
for (int t = ctrl.Controls.Count - 1; t >= 0; t--)
{
Control c = ctrl.Controls[t];
c.PreviewKeyDown -= HandlePreviewKeyDown;
c.PreviewKeyDown += HandlePreviewKeyDown;
c.MouseDown -= HandleMouseDown;
c.MouseDown += HandleMouseDown;
HookupNavigationButtons(c);
}
}
And call that method somewhere after InitializeComponent(); with HookupNavigationButtons(this);
If you only want the mouse events you can leave out the keyboard stuff, but there are a few keyboards out there that also have these navigation buttons.

How to go to previous page in windows phone 8 when back button is pressed

I have a home page or landing page in my windows phone c# based app where user enters login details and upon successful login user is redirected to page2 . Here the user will see a list box with few items . Upon selecting an item from this list box a new page called "Threadx" opens.(where x is the each page that opens upon clicking the x item in the list box)
While user is on this Thread page "Threadx" he may receive the toast notifications and the thread gets updated with new replies or answers on that thread.
But When user clicks on back button the "ThreadX" page doesn't get closed and instead it goes to its previous state where it has less number of messages , and so on until the app gets closed.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
return;
}
}
I would like to know if this "Threadx" page can be closed upon clicking back button without affecting other "Threadx+1","Threadx+2"..."Threadx+n" pages.
Any help would be really appreciated.
Normally windows keeps the pages on it's stack when you leave a page and navigate to another page. If you want to navigate to the previous page on pressing the Back Button you can do following things:
Add following line to OnNavigatedTo method:
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Add definition for HardwareButtons_BackPressed method:
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
if (Frame.CanGoBack)
Frame.GoBack();
}
Don't forget to add Windows.Phone.UI.Input to the namespace list.
The other way I got it worked was using the below code in the onnavigateto method in my "thread" page and it worked for me. Let me know if there is an elegant way of doing it or better way of doing it .
if (e.NavigationMode == NavigationMode.Back)
{
NavigationService.Navigate(new Uri("/View/Page2.xaml", UriKind.Relative));
}

Back button control in windows phone app

I am developing a windows phone 8 app. I want to control the back button of the phone for doing specific task. I want that when user press the back button in specific page it will not navigate to the previous page but to the page which I want. Is their any way to control the hardware back button present in phone?
In Silverlight apps (WP7, WP8, WP8.1) you do this:
protected override void OnBackKeyPress(CancelEventArgs e)
{
// put any code you like here
MessageBox.Show("You pressed the Back button");
e.Cancel = true;
}
That will work in all Windows Phone versions if you're using Silverlight.
If you're using WinRT for Windows Phone 8.1, it is a bit different:
Open NavigationHelper.cs and make this modification:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (this.GoBackCommand.CanExecute(null) && !e.Handled)
{
e.Handled = true;
this.GoBackCommand.Execute(null);
}
}
Now in your app page (the page that will be open when the back button is pressed), add the following namespace:
using Windows.Phone.UI.Input;
Add this handler to the constructor method of your page:
HardwareButtons.BackPressed += OnBackPressed;
Then add this method:
private async void OnBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
e.Handled = true;
// add your own code here to run when Back is pressed
}
Note: in both cases, the 'e.Handled = true' line tells the OS that the back button press has been handled, and therefore the OS will not action the default behaviour. If you remove that line your own code will run, and the OS will also do its own backwards navigation.
Be mindful of Rowland's comment about overriding the Back button - if you're not navigating intuitively you will confuse the user and risk your game being rejected (if you just need to control a pause screen or menu it will be fine, but if you implement something gimmicky like using the Back button as a game control you'll be in trouble).
My blog has the same answer with a bit more detail if you need it:
http://grogansoft.com/blog/?p=572
Whilst it possible to cancel the navigation event, and permissable in a game to present a pause screen or similar, generally it is not allowed to use the back button for anything other than backward navigation in an app; Per requirement 5.2.4 of the Technical certification requirements for Windows Phone
To maintain a consistent user experience, the Back button must only be used for backwards navigation in the app.
If you are creating a XAML app where it is permissible to cancel a "back" operation, such as per 5.2.4.4 of the Technical certification requirements for Windows Phone
:
For games, when the Back button is pressed during gameplay, the game can choose to present a pause context menu or dialog, or it can navigate the user to the prior menu screen.
Then you can implement this by overriding the OnNavigatingFrom method on your page, and set the Cancel property of the NavigatingCancelEventArgs, so something like this example from Frame, page, and navigation features for Windows Phone 8:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
// If the navigation can be cancelled, ask the user if they want to cancel
if (e.IsCancelable)
{
MessageBoxResult result = MessageBox.Show("Do you want to stay here?", "Confirm Navigation from Page", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// User wants to stay here
e.Cancel = true;
return;
}
}
}
Of course, you may choose to implement the prompt differently, but that should illustrate how it is possible.

How to determine what the back button does in universal apps

I am writing a universal app and when I am testing it on the windows phone emulator when the back key is pressed it just brings me back to the start screen instead of navigating back a page.
This is the first windows phone 8.1 app I have made and I need some help on how to set so that the back key takes you back an app page instead of bringing you out of the app.
You need to handle the HardwareButtons.BackPressed event and plug into your app's navigation system. Commonly you'll find the Frame object, check if frame.CanGoBack, and if so call frame.GoBack. If you're at the app's front page (frame.CanGoBack is false) then don't handle the event and let it back out of the application.
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null)
{
return;
}
if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
See Handling the Back button in a Windows Phone app
The NavigationHelper.cs classes in the non-blank Windows Phone app templates will hook this up for you.

How can i override back button as I used to?

I've just installed Windows Phone 8.1 SDK, and had an application in mind. But I cant even navigate back and forth! Back button the phone exit the application by default, and since all the pages now inherits "Page" the override for the back button isnt exposed.
Read http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn639128.aspx but I don't understand it, how can I implement it?
Take a look at any of the WP Projects that are included with Visual Studio (eg: The Hub App project). Or add a new "BasicPage" to your application. You will notice that they are using a NAvigationHelper to subscribe to the BackPressed event for you already. The post you linked to explains it pretty well.
The most important thing to know about the BackPressed event that is raised when the user presses the back button is that if your app does not handle the event, by setting BackPressedEventArgs.Handled property to true, the operating system will suspend your app and return the user to the previous experience
The example is given in that post
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null)
{
return;
}
if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
Notice it sets e.Handled = true; to indicate that the app should not "close". You are saying "Hey, I've got this handled already". In the example, it will navigate to the previous page.

Categories