In WinRT XAML - tapping and clicking on a button i

2019-07-30 06:31发布

问题:

When hooking the Tapped event on a page (so I can get all events that are not caught by children), I am also getting the event fired when a user taps a button on the page.

I would have expected the button to "swallow" the tap, but that does not seem to be the case.

The workaround is simple, if annoying - hook the Tap event on each button present and set Handled to true. I get how to do the workaround.

My question is whether or not I am understanding this correctly. If hooking the Tap event on all my buttons that are on the page (so I don't get the tap event on the page fired) really the correct way to do this.

Thanks.

回答1:

You should handle the Click event on the button, not the Tapped event. If the Tapped event still bubbles to the page - yes, by all means, handle it on the button and mark it Handled to prevent bubbling or try to change the IsTapEnabled value on the button. If handling the event is the only way and you don't like the code behind - you can either have all button Tapped events use the same handler or define an attached behavior that does the swallowing in a separate class where you define the behavior.