Why would my event handler not get called?

I've got this xaml:

<Button VerticalAlignment="Bottom"
        Click="SaveAndGoBack"
        IsEnabled="{Binding Frame.CanGoBack,
                            ElementName=pageRoot}"
        Style="{StaticResource BackButtonStyle}" />

...but on "tapping" (clicking) the back button, while it does return to the previous page, SaveAndGoBack() is not called - I have a breakpoint on the first line, and it is not reached.

The button is obviously enabled, because I am able to select it and "go back." So why would the event handler not be reached?

UPDATE

Changing clicked to tapped makes no difference. This is the XAML now:

<Button VerticalAlignment="Bottom"
        Tapped="SaveAndGoBack"
        IsEnabled="{Binding Frame.CanGoBack,
                            ElementName=pageRoot}"
        Style="{StaticResource BackButtonStyle}" />

...and this is the method, with a breakpoint on the first line, which is never reached (but the Main Page is returned to):

private async void SaveAndGoBack(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs args)
{
    SOs_Locations sos_locs = new SOs_Locations { GroupName = txtbxGroup.Text };
    int locNum;
    int.TryParse(txtbxLocationNum.Text, out locNum);
    sos_locs.LocationTitle = txtbxTitle.Text;
    sos_locs.CivicAddress = txtbxAddress.Text;
    double lat;
    double.TryParse(txtbxLatitude.Text, out lat);
    sos_locs.Latitude = lat;
    double lng;
    double.TryParse(txtbxLongitude.Text, out lng);
    sos_locs.Longitude = lng;
    if (cmbxDisplayColor.SelectedValue != null)
        sos_locs.LocationColor = cmbxDisplayColor.SelectedValue.ToString();
    await SQLiteUtils.UpdateLocationAsync(sos_locs);
    Frame.Navigate(typeof(MainPage));
}

Upvotes: 2

Views: 613

Answers (2)

scartag
scartag

Reputation: 17680

Click is a different event from a Tap. you should look for an event called Tapped and create an OnTapped handler for it.

My guess is that for your issue. The event isn't bubbling because in the LayoutAwarePage.cs the event is set to handled.

You can modify this page to suit your needs. Look for the CoreWindow_PointerPressed method and look for the area where it sets args.Handled = true

Upvotes: 4

mlorbetske
mlorbetske

Reputation: 5649

Use the Tapped event instead as it handles click, tap and stylus events whereas Click deals specifically with the mouse (see this answer for more details).

Here's some information on how they've tried to unify the events around the different pointing input modalities around touch.

Upvotes: 1

Related Questions