so I'm creating a reaction time testing app using visual studio xamarin.forms. I have finished my drafted code, however, I have a problem where my reaction time (according to the stopwatch) is 100ms greater than what i have tested on other reaction time testers. I tried getting the stopwatch to stop when the button is pressed instead of clicked however that made insignificant changes to the output time. Has anybody seen something like this before or am i missing something? Thanks
This is the xamarin side of the reaction time test.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ReactionTimeA.ThirdPage">
<ContentPage.Content>
<StackLayout Padding="20,35,20,20">
<Label x:Name="Header_Label"
Text="When the light turns green remove finger from button"
FontSize="24"
Margin="30"
TextColor="Black"/>
<Button x:Name="ReactionTimeButton"
Text="Start Count"
Pressed="OnButtonClicked"
WidthRequest="100"
HeightRequest="100"
VerticalOptions="Center"
HorizontalOptions="Center"
CornerRadius="75"
BackgroundColor="Blue"
Margin="30"/>
<Label x:Name="prereactionLabel"
Text=""
TextColor="Black"
FontSize="12"/>
<Label x:Name="reactionLabel"
HorizontalTextAlignment="Center"
FontSize ="72"
Text = ""
TextDecorations = "Underline"
TextColor="Black"
Margin="30"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
This is the c# side of the code which i assume has the most problems in it. Note im pretty crappy at making my code succinct so forgive me for that.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ReactionTimeA
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ThirdPage : ContentPage
{
int button_count = 0;
Stopwatch stopwatch = new Stopwatch();
public ThirdPage()
{
InitializeComponent();
}
public void ReactionTime() {
Random rand = new Random();
ReactionTimeButton.IsEnabled = false;
int time = rand.Next(1000, 10000);
Task.Delay(time).Wait();
}
void OnButtonClicked(object sender, EventArgs e)
{
button_count++;
if (button_count == 1)
{
ReactionTimeButton.Text = "Waiting";
ReactionTime();
ReactionTimeButton.IsEnabled = true;
ReactionTimeButton.BackgroundColor = Color.Lime;
ReactionTimeButton.Text = "Click Now";
stopwatch.Start();
}
else if (button_count == 2)
{
stopwatch.Stop();
int elapsed_time = (int)stopwatch.ElapsedMilliseconds;
string output_time = Convert.ToString(elapsed_time);
prereactionLabel.Text = "Your reaction time is:";
reactionLabel.Text = output_time + "ms";
button_count = 0;
stopwatch.Reset();
ReactionTimeButton.Text = "Start Count";
ReactionTimeButton.BackgroundColor = Color.Red;
button_count = 0;
}
}
}
}
Button.Pressed event will be raised when you release a finger from the button. It seems that you are looking for an even to be raised when you touch the button instead. You can use gesture recognizers for that or an approach by creating native renderers and handling relevant button touch events natively, described here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With