Basically, I'm trying to get the canvas to listen to touch input (tap) and will increase the number of taps on the screen. It does not work when I touch the screen on my device. I debugged my code, and there was nothing unusual, except that the sensor was not detected. I checked ZIndex and the canvas is in front of the screen to be tangible. How can I make it work?
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" Margin="188,10,187,397"/>
<Button Content="Reset" HorizontalAlignment="Stretch" Margin="-18,535,-18,0" VerticalAlignment="Top" Click="Button_Click"/>
<Canvas ZIndex="0" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseLeftButtonUp="Canvas_MouseLeftButtonUp" MouseLeave="Canvas_MouseLeave"/>
</Grid>
WITH#
int taps = 0;
public MainPage()
{
InitializeComponent();
}
private void Canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
counter.Text = "TOUCHED!";
}
private void Canvas_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
taps++;
counter.Text = taps.ToString();
}
private void Canvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
MessageBox.Show("You left the screen without lifting your finger. That does not count as a tap!", "Caution!", MessageBoxButton.OK);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
taps = 0;
counter.Text = taps.ToString();
}
source
share