【问题标题】:Stopwatch in visual studio xamarin.forms adding 100ms to reaction time testVisual Studio xamarin.forms 中的秒表在反应时间测试中增加了 100 毫秒
【发布时间】:2020-12-02 11:48:22
【问题描述】:

所以我正在使用 Visual Studio xamarin.forms 创建一个反应时间测试应用程序。我已经完成了我起草的代码,但是,我有一个问题,我的反应时间(根据秒表)比我在其他反应时间测试仪上测试的时间长 100 毫秒。我尝试让秒表在按下按钮而不是单击时停止,但这对输出时间的影响微乎其微。有没有人见过这样的东西或者我错过了什么?谢谢

这是反应时间测试的 xamarin 方面。

<?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>

这是我认为问题最多的代码的 c# 部分。请注意,我在使我的代码简洁时非常糟糕,所以请原谅我。

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;
            }
        }
    }
}

【问题讨论】:

  • 你为什么要启动和停止秒表?只需记下当前时间,最后减去它们。

标签: c# xamarin xamarin.forms


【解决方案1】:

Button.Pressed 事件将在您从按钮上松开手指时引发。当您触摸按钮时,您似乎正在寻找一个偶数。您可以为此使用gesture recognizers,或者通过创建原生渲染器并原生处理相关按钮触摸事件的方法,described here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2017-04-25
    • 1970-01-01
    • 2017-06-27
    • 2012-02-12
    相关资源
    最近更新 更多