1

I have a XAML user control with a collection of buttons.

My XAML is as follows:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" Width="85"
>

<Grid Background="#7F010305" HorizontalAlignment="Left">
    <Grid.RowDefinitions>            
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />            
    </Grid.ColumnDefinitions>

    <Button x:Name="Zoom" Grid.Row="0" Grid.Column="0" Width="40" Height="40" Margin="15,0,15,15" Click="OnClicked" Tapped="OnClicked" />
    <Button x:Name="Pan"  Grid.Row="1" Grid.Column="0" Width="40" Height="40" Margin="15,0,15,15" Click="OnClicked" Tapped="OnClicked" />
    <Button x:Name="Contrast" Grid.Row="2" Grid.Column="0" Width="40" Height="40" Margin="15,0,15,15" Click="OnClicked" Tapped="OnClicked"/>
    <Button x:Name="Brightness" Grid.Row="3" Grid.Column="0" Width="40" Height="40" Margin="15,0,15,15" Click="OnClicked" Tapped="OnClicked"/>

</Grid>

Somehow the click or the tap event is not getting raised. I tried various options but the Click or Tapped event is not getting fired.

Any hints?

Venki
  • 2,129
  • 6
  • 32
  • 54

1 Answers1

1

From looking at your code I notice that you have both the Click and Tapped event handlers are pointing to the same method ("OnClicked"). These events, however, have different signatures for delegates. The Click event expects a method with signature (http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.primitives.buttonbase.click.aspx):

void ClickDelegate(object sender, RoutedEventArgs e)

while the Tapped event expects a method with signature (http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.input.tappedeventhandler.aspx):

void TappedDelegate(object sender, TappedRoutedEventArgs e)

So the signatures of the two methods aren't equivalent and it seems unlikely that you would be able to use the same method for both events since they require different delegate signatures (The code that runs behind the scenes to merge the logic in your xaml to your code behind should be looking for a method void OnClick(object, RoutedEventArgs) for your Click handler and a method void OnClick(object, TappedRoutedEventArgs) for your Tapped handler). Also if the runtime doesn't find a matching handler method (at least for wirnt in 8.1) I have found through testing that the event handler hook up will silently fail rather than throwing an exception (I am pretty sure that for WPF this causes an error, which makes it slightly confusing).

Long story short without being able to see your code behind my best guess is that OnClick doesn't have a signature that matches the delegate types of either the Clicked or Tapped event. Beyond that I am also wondering why you hooked into both Tapped and Clicked. Tapped is the generalized event that basically does the same thing as Clicked and then some (see Are Click, Tapped, and PointerPressed synonymous in WinRT-XAML?). I can't say for sure whether or not hooking into both of them may cause some weird issues (such as both the Clicked and Tapped being called for a single press) but it seems like you would probably just want to be subscribing to the Tapped event.

EDIT:

Just to add another point. Sometimes I have had it happen that even if you do everything right (where right here equals you have painstakingly double checked everything and it is all correct as far as you can tell) the event handler still doesn't work. Honestly I have only ever seen this happening when there are much more intricate combinations of components than what you have (such as those involving pretty much every custom type of thing you can imagine... custom panels, datatemplateselectors, itemtemplateselectors, etc) and am not sure if I am just constantly overlooking something from time to time or if there are legitimate bugs in the Microsoft code.

Anyway, the workaround is always to avoid hooking it up the event handler in the 'normal' way (like what you are doing here). To do this give the button a name and add the handler in the code behind (either by directly using the name if the button isn't in a template or by using GetTemplateChild from OnApplyTemplate to grab it if it is in a template). Then you can just do something like this for a button named 'foo':

foo.Tapped += (sender, args) => { /*do something here */ };
Community
  • 1
  • 1
Chiune Sugihara
  • 1,179
  • 1
  • 7
  • 14