0
votes

I am using Xamarin.Forms for Android app development and would like to create a counter whenever the player taps the screen. How do I make a counter to count the number of taps on the screen? I am using Visual Studio 2019 if that helps. Thank you!

Here's my xaml.cs code (don't mind the InitializeComponent and the number of curly braces. Those are fine): enter image description here

Here's my .xaml code: enter image description here

2
Do you understand how to respond to a tap event? Do you understand how to create a counter variable and increment it?Jason
I don't understand either. I am very new to this.SavageGames24

2 Answers

0
votes

Can you do this

In XAML

<StackLayout Spacing="15">
    <!-- Place new controls here -->
    <Label Text="0" x:Name="lblCount" 
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

    <Button Text="Increment" Clicked="Increment"/>
</StackLayout>

In XAML.cs

public partial class MainPage : ContentPage
{
    int lblValue = 0;
    public MainPage()
    {
        InitializeComponent();
    }
    private void Increment(object sender, EventArgs e)
    {
        lblValue++;
        lblCount.Text = lblValue.ToString();
    }
}

That's a simple way to solve this, if you need uses Binding or other feature tell me.

Just like Jason said I suggest you look at the documentation

0
votes

You can add gesture recognizer to your main frame(stacklayout in example)

XAML

<StackLayout BackgroundColor="#313FA0">
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnTapped"/>
    </StackLayout.GestureRecognizers>

 //All page stuff


</StackLayout>

C#

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();
    }

   int taps = 0;
    private void OnTapped(object sender, EventArgs e)
    {
        taps++;
        Console.Writeline(taps.ToString());
    }
}