简体   繁体   中英

How to suppress the Click event in extended Button control?

I have a custom button in my Windows Phone application. The button overrides the OnClick method to prevent a regular click routine:

protected override void OnClick()
{
    if(ProgressBar != null
        && DelayDuration.HasValue
        && DelayDuration.Value.HasTimeSpan
        && DelayDuration.Value.TimeSpan > TimeSpan.Zero)
    {
        // Suppress the default click action
        return;
    }

    base.OnClick();
}

Now I need to convert my app to the Windows Store one. But the Button class doesn't have the OnClick method! How can I extend Button behavior from the derived control?

There is Click event for Button. http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.controls.primitives.buttonbase.click.aspx

You can override button's OnTap method in Windows Phone or Windows Store application

protected virtual void OnTapped(
  TappedRoutedEventArgs e
)

and set e.Handled = true if you want to prevent event tunneling.

For all practical purposes, the Tap and Click events are equivalent for a Button.

The Click event was originally defined in Silverlight for desktop Windows and it is only defined for the Button control (and derivatives such as HyperlinkButton). You can think of the Click event as the "traditional" way to handle a Button press.

The Tap event was added to the framework in Windows Phone 7.1 (Mango). Tap is defined in the UIElement class, which is the parent of many types of controls. You can handle a Tap event in a TextBlock, Image, and many other controls. Button is subclassed from UIElement as well, and thus can also receive Tap events. It is redundant that a Button can receive both Tap and Click events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM