BackRequested event handler event.Handled is ignored - c#

So, as per the docs I attach a handler when I navigate to a page:
protected override void OnNavigatedTo(NavigationEventArgs e) {
/* Attach back listener to handle the back press */
SystemNavigationManager.GetForCurrentView().BackRequested += NavigationPage_BackRequested;
...
}
And I detach it when leaving:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) {
SystemNavigationManager.GetForCurrentView().BackRequested -= NavigationPage_BackRequested;
e.Cancel = false;
}
And I mark the event as handled to prevent the system from handling it:
private void NavigationPage_BackRequested(object sender, BackRequestedEventArgs e) {
e.Handled = true;
}
But the system still handles the back click and I get navigated away. Why?

Your code shown above is working fine (tested on my machine), but the big question is: do you try to cancel a system provided navigation event or a Frame.GoBack() event implemented by the software button of your application?
SystemNavigationManager: Provides a way for an app to respond to system provided back-navigation events.
If you look at the backwards navigation documentation, only certain back buttons (hardware/software) are system provided events, e.g. the software button at the bottom in Tablet mode.
However quite a few project templates (in Visual Studio or from MVVM libraries) also provide a software back button (quite often at the top left) and wire this event to the Frame.GoBack() method. This is NOT a system provided event and can't be cancelled this way. Reasoning: You (or the framework used) is calling the GoBack() explicitly, so why would it have to be cancelled in this scenario.

Related

Check if user came to app by clicking on SecondaryTile

I want to know if user opened app by clicking SecondaryTile (and also which one was clicked).
Now I have OnNavigatedTo method:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if (!String.IsNullOrEmpty(e.Parameter.ToString()))
{
//e.Parameter is not null, LiveTile was used
//do something
}
else
{
//No of SecondaryTiles were clicked
}
}
This of course works, but only if app was previously closed. But when app was previously opened, runs in background and user click on LiveTile, then app is being shown but this method is not executed.
How can I handle this scenario?
MSDN: Override the OnLaunched method to perform any general app initialization that should occur only when the user launches your app normally (for example, by tapping the app tile).
You should override or update the OnLaunched event to track app activation moment and analyze corresponding arguments (app.xaml.cs file).
In short, if you look on how OnNavigatedTo event is rised -- it comes from the OnLaunched event that checks whether rootFrame's content already exists or not:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
...
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
...
}
As you may imagine in your scenario it is not true, so navigation event never happens.

Passthrough all mouse events but not stylus events

I am making an application for personal use where the stylus can be used to draw on the current screen but the normal use (with mouse) won't be interrupted.
Currently, I am trying to use WS_EX_TRANSPARENT to set the window to allow mouse events through, but it seems like that stylus events also get passed through without being captured.
Is there any other method I can use to pass through mouse/keyboard events while still allowing stylus events? Here is what my program looks like so far:
Disable the RealTimeStylus for WPF Applications on MSDN states:
[...] (WPF) has built in support for processing Windows 7 touch input [...] Windows 7 also provides multi-touch input as Win32 WM_TOUCH window messages. These two APIs are mutually exclusive on the same HWND.
This seems to imply that, for a given window, you can receive stylus events or touch events but not both. As you do want to handle the stylus events this means you don't need to bother filtering the touch events. That just leaves the mouse and keyboard.
At first I thought you might be able to use a custom window procedure (WndProc) and filter-out the mouse and keyboard messages. However, the WndProc (when used in WPF) is really just a notification mechanism and you can't block the received messages.
I found a Windows API called BlockInput that supposedly "Blocks keyboard and mouse input events from reaching applications". However from the docs this appears to be system-wide not app-specific so may not be any use to you.
The only other way I can think of is to use a low-level keyboard or mouse hook. This requires some P/Invoke but it's not too difficult. These hooks allow you to register callback functions that get called when keyboard and mouse events are raised. The advantage is that you can prevent those events from propagating and effectively "swallow" them, which sounds like what you need.
I don't really like posting an answer that basically says "do a search for ..." but the amount of code involved is non-trivial and has been posted in numerous places both on Stack Overflow and elsewhere, so: try doing a search for low level keyboard hook c# wpf and you should find some code that might help!
One thing you may have trouble with even if you go down this route is focus. As soon as your "invisible" topmost window gets a stylus message that it responds to, I'm presuming focus will switch to your WPF application, thus "stealing" focus from whatever application was being used prior. You might be able to use P/Invoke again to set the window style flags of your main window to prevent this (as per the accepted answer to this SO question).
ORIGINAL ANSWER
You can override the appropriate keyboard, mouse and touch Preview... event handler methods of the Window and mark them as handled. This has the effect of stopping child controls from receiving those events.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
e.Handled = true;
base.OnPreviewKeyDown(e);
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
e.Handled = true;
base.OnPreviewMouseDown(e);
}
protected override void OnPreviewMouseMove(MouseEventArgs e)
{
e.Handled = true;
base.OnPreviewMouseMove(e);
}
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
{
e.Handled = true;
base.OnPreviewMouseWheel(e);
}
protected override void OnPreviewTouchDown(TouchEventArgs e)
{
e.Handled = true;
base.OnPreviewTouchDown(e);
}
protected override void OnPreviewTouchMove(TouchEventArgs e)
{
e.Handled = true;
base.OnPreviewTouchMove(e);
}
}
I've done the basic keyboard, mouse and touch events here. In a simple app test it seemed to do the trick and I assume it would still let stylus events through (I don't have a stylus I can test with).
You may have to experiment with which events need to be handled like this. I only did KeyDown for example, not KeyUp as I presume the latter is irrelevant without the former. I may also have implemented some that didn't need to be handled, and I'm not sure the calls to the base methods are needed either. As I say, experiment until you get something that works for you.

Session Ending event not working

I want to let the user decides if he wants to do a last request at my app when the the Windows is shutting down or logging off. So I am using the "SessionEnding" event.
The following code is triggered but doesn't work. Here is the simplest example:
public App()
{
this.SessionEnding += App_SessionEnding;
}
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
e.Cancel = true;
}
I am afraid that I've used a boost program before in my computer (like CCleaner), and somehow it deactivated this event. =O
When I add a MessageBox to the event, and I request to log off my computer, then the MessageBox appears, but I don't have time to click somewhere because after 1 second, the system log off. The system seems to not be waiting for my application.
Obs: I am using Windows 7.
I tried a sample of your code without success ... however I put this into my main window that causes the application to close and the MessageBox was displayed indefinitely until I clicked close. Could this help?
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Blah", "Blah", MessageBoxButton.OK);
base.OnClosing(e);
}
Try overriding the event handler that's already there.
https://wpf.2000things.com/2011/01/25/197-override-application-class-methods-for-standard-events/
public partial class App : Application
{
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
// Always call method in base class, so that the event gets raised.
base.OnSessionEnding(e);
// Place your own SessionEnding logic here
}
}

Assign Form1_Closing method to Form Closing Event

I have the following method defined to be called when a form closes
private void TimeKeeper_Closing(object sender, EventArgs e)
However, it doesn't show up in the list when I try to assign it in the Events section of the Form properties. Is there something I need to do to get it to show up there?
EDIT: Sorry, originally posted the wrong method...
The Closing event is obsolete, it dates from .NET 1.x. Microsoft goofed that one pretty badly and it was replaced in .NET 2.0 with the FormClosing event. Which tells you a lot more about why the form is getting closed. The e.CloseReason is very important, you don't want to prevent Windows from shutting down.
Which is why you can't find it, you are not supposed to use it anymore. Note how the answer you accepted just stops your program from compiling. You probably discovered the FormClosing event by yourself.
Might as well go whole-hog and point out how silly it is for a class to listen to its own events. Events are meant for other code. They work pretty well in the designer, that's why you end up writing code like this. But the sane thing to do is to just override the method:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
// Some code that might set e.Cancel = true
//...
}
base.OnFormClosing(e);
}
Which has many advantages, beyond the typical lossage of forgetting to subscribe the event with the designer, a derived class can simple alter the decision being made here by setting e.Cancel back to true.
You have to change EventArgs to FormClosingEventArgs
private void TimeKeeper_Closing(object sender, FormClosingEventArgs e)

Intercept every mouse click to WPF application

I'm looking to intercept every mouse click in my WPF application. Seems this should be easy with the command routing mechanism, but sorry I'm not finding anything.
My application implements several security levels, and has the requirement to automatically revert to the most restrictive level if no one interacts with (clicks) the application in x minutes. My plan is to add a timer that expires after x minutes and adjusts the security level. Each mouse click into the application will reset the timer.
You can register a class handler:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(Window), Window.PreviewMouseDownEvent, new MouseButtonEventHandler(OnPreviewMouseDown));
base.OnStartup(e);
}
static void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Trace.WriteLine("Clicked!!");
}
}
This will handle any PreviewMouseDown event on any Window created in the application.
<Window .... PreviewMouseDown="Window_PreviewMouseDown_1">
</Window>
This should work for you.
This fires even if other MouseDown events fire for components that it contains.
As per Clemens suggestion in the comments, PreviewMouseDown is a better choice than MouseDown, as that makes sure you can't stop the event bubbling from happening in a different event.
You have a few options:
Low level mouse hook:
http://filipandersson.multiply.com/journal/item/7?&show_interstitial=1&u=%2Fjournal%2Fitem
WPF Solution (I'd check to see if this does what you need first):
WPF. Catch last window click anywhere

Categories

Resources