How to make canvas detect touch input correctly in C #?

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; // create var to detect number of times, user touches the screen

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    // method to register the touch as the finger is placed on the screen
    private void Canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        //Canvas c = sender as Canvas;
        counter.Text = "TOUCHED!";
    }

    //method register the touch as the finger is lifting up from the screen
    private void Canvas_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        //Canvas c = sender as Canvas;
        taps++;
        counter.Text = taps.ToString(); //convert var from int to string
    }

    //method register the touch as the finger leaves the area of the screen
    private void Canvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        //Canvas c = sender as Canvas;
        MessageBox.Show("You left the screen without lifting your finger. That does not count as a tap!", "Caution!", MessageBoxButton.OK);
    }

    // method to reset the counter to zero when button is pressed and released
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        taps = 0; // reset the count
        counter.Text = taps.ToString(); // convert var from int to string
    }
+3
source share
4 answers

, Canvas - , Canvas , /, Canvas . , , MouseUp/Down - Grid, , Grid Canvas:

XAML:

<Grid x:Name="ContentPanel"  Margin="12,0,12,0" Background="Transparent">
    <Grid.RowDefinitions>
       <RowDefinition Height="7*"/>
       <RowDefinition Height="1*"/>
       <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" Grid.Row="0"/>
    <Button Content="Reset" HorizontalAlignment="Stretch" VerticalAlignment="Center" Click="Button_Click" Grid.Row="1"/>
    <TextBlock Name="Touched" FontSize="50" HorizontalAlignment="Center" TextWrapping="Wrap" Text="Touched" VerticalAlignment="Center" Visibility="Collapsed" Grid.Row="2"/>                
</Grid>

:

private int taps = 0;
public MainPage()
{
    InitializeComponent();

    ContentPanel.MouseLeftButtonDown += ContentPanel_MouseLeftButtonDown;
    ContentPanel.MouseLeftButtonUp += ContentPanel_MouseLeftButtonUp;
}

private void ContentPanel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
   taps++;
   counter.Text = taps.ToString(); //convert var from int to string
   Touched.Visibility = Visibility.Collapsed;
}

private void ContentPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    Touched.Visibility = Visibility.Visible;
}

// method to reset the counter to zero when button is pressed and released
private void Button_Click(object sender, RoutedEventArgs e)
{
   taps = 0; // reset the count
   counter.Text = taps.ToString(); // convert var from int to string
}

, Grid ( ) - , , , .

, , .

+2

, ? MouseLeftButtonDown MouseLeftButtonUp TouchDown TouchUp.

, . MouseLeftButtonDown. , . , TouchDown/TouchUp.

+1

, - .

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <stackpanel>
            <TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" Margin="0,0,0,0"/>
            <Canvas ZIndex="0" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseLeftButtonUp="Canvas_MouseLeftButtonUp" MouseLeave="Canvas_MouseLeave"/>
            <Button Content="Reset" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Top" Click="Button_Click"/>
    </stackpanel>
</Grid>

.

0

. , - Transparent:

<Canvas ZIndex="99" Background="Transparent" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" Tapped="Canvas_CountMyTaps"/>

(If you want the canvas to be on top, be sure to make it larger ZIndex than other elements that it overlaps)

If not set (the default value is null), the element will not capture any clicks / clicks, etc., it will be as if they had "failed."

Also, consider using a Tapped event, which is a higher-level event that will respond to clicks, taps with your finger, stylus, etc.

0
source

All Articles