I have some basic app on windows phone 8.1, and in that i have regular buttons, for navigate or exit application. I want to disable HardwareButton for back/exit, so if anyone press it, application will not exit.
Any help?
Btw, i tried:
Public void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
e.Handled = false;
}
You have to rewrite the HardwareButtons_BackPressed method on App.xaml.cs file.
Also if you have handled the Event, you have to set e.Handled = true to tell the system you have already handled the event and dont push the event below in the queue.
For more and examples see MSDN.
Related
I have UWP app that implements the below code to wire up the system back button. My understanding is that this event is provided to capture hardware back buttons on Windows Phones, the back button in the title bar on Windows 10 and the back button on the task bar in Windows 10 tablet mode.
The hardware and title bar back buttons are working in my app, but when in tablet mode, pressing the back button on the task bar moves my app to the background and navigates to the Start Menu regardless of where I am in the app backstack. The BackRequested event IS firing in this case and my app is navigating back one page.
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested +=
App_BackRequested;
}
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
NavService.GoBack();
}
Any thoughts on why the tablet mode back button would behave this way? I'm seeing this behavior across many Windows 10 PCs, Surfaces, etc.
The default behavior of the Tablet mode back button is indeed to navigate out of the app. To prevent this you have to make sure that when you can navigate back in the app, you also mark the back navigation as handled.
private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
if ( NavService.CanGoBack() )
{
NavService.GoBack();
e.Handled = true;
}
}
You will have to add a CanGoBack() method that will check the app Frame's CanGoBack property.
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.
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.
I have C# app that monitors keystrokes via KeyDown and KeyPress events. Specifically, it watches for a VolumeMute keystroke (from a handheld device) to do some special processing. It works fine with one problem: Windows seems to intercept the VolumeMute keystroke and mutes the system volume (which I don't want to happen). I think Windows intercepts the keystroke before it is processed by my app because even when I signal the keystroke was handled (e.Handled = true), it mutes the system volume anyway. BTW, the same code works perfectly for other keystrokes I'm catching (ex Backspace, ect).
Is there a way to stop Windows from doing this volume mute?
System: WinXP SP3, .Net 4 Client Profile, Windows Forms app
Code snips
bool keyHandled = false;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (keyHandled)
{
e.Handled = true;
}
}
// =====================================
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
keyHandled = false;
switch (e.KeyCode)
{
case Keys.VolumeMute:
DoSpecialProcessing();
keyHandled = true;
break;
default:
break;
}
e.Handled = keyHandled;
}
The button acts like a toggle, it will mute on the first press and "un-mute" on the next press. All you need is to simulate VolumeMute press again.
This answer explains how to do it.
Note: If you call this method from inside KeyDown handler then you probably need to use BeginInvoke or PostMessage to post a message to a message queue and return immediately in order to avoid race conditions.
Ok. I ended up using AutoHotKey to remap the VolumeMute to another key. Importantly, Windows never sees the VolumeMute keystroke before it gets remapped and doesn't mute the sound. So I can just start this AutoHotKey remap script when I need it. Beats messing with the Windows Registry. Thanks for the suggestions.
I am new to Windows Mobile and created an app that needs to do some clean up when a Form is closed. When I click on the Close in the ControlBox it does not call the OnClosing event. This works fine in regular windows but is not working with my Windows Mobile device.
Here is my code:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
...
}
I have also tried this with no luck:
Form.Closing += new System.ComponentModel.CancelEventHandler(WindBaseForm_Closing);
I would appreciate any help you can give.
Correct. The (X) button in Windows Mobile is called the "Smart Minimize" button and only minimizes the Form. Set the Form's MinimizeBox property to false and it will change to (OK) which will close the Form (and raise the event).
For the gory details as to why this happens, read this.