编程语言
首页 > 编程语言> > 如何在C#中使画布正确检测触摸输入?

如何在C#中使画布正确检测触摸输入?

作者:互联网

基本上,我正在尝试使画布侦听触摸输入(点击),并将增加屏幕上的点击次数.当我触摸设备上的屏幕时,它不起作用.我调试了代码,除了未检测到触摸外,没有其他异常.我检查了ZIndex,并且画布在屏幕前面,可以触摸.我该如何运作?

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>

C#:

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
    }

解决方法:

我不知道您为什么要使用Canvas进行操作-它在此Canvas中一无所有,因此无法使用,因此它无法注册您的点击/轻按,Canvas也是hard to adjust to screen.我认为可以更简单地完成此操作如果要使用MouseUp / Down进行操作-直接订阅包含您的元素的Grid,而不是用其他Canvas填充此Grid:

在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事件(它涵盖了整个屏幕)-但是要使其工作,我必须将其Background Brush设置为Transparent,否则只有在您触摸文本时它才起作用.

还有许多其他方法可以使您的App正常工作,但我希望这会有所帮助.

标签:visual-studio-2013,canvas,windows-phone-8,xaml,c
来源: https://codeday.me/bug/20191122/2057406.html