【问题标题】:Xamarin Forms Stopwatch Milliseconds FormatXamarin 形成秒表毫秒格式
【发布时间】:2021-04-05 20:08:34
【问题描述】:

我正在使用 xamarin 形式的简单秒表, 一切都很好,但是当我使用 lable 查看它时,它显示毫秒包括(7)位数字,如下所示: 00:00:00:0000000

我怎样才能减少它或按我的意愿格式化它?

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
            Device.StartTimer(TimeSpan.FromMilliseconds(0), () =>
            {
                lblText.Text = stopWatch.Elapsed.ToString();
                return true;


            });

【问题讨论】:

标签: c# xaml xamarin.forms


【解决方案1】:

您可以使用string.Format 格式化。

// Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);

更新:

Xaml:

  <StackLayout>
        <Label x:Name="lbl_result" FontSize="Large" />
        <StackLayout Orientation="Horizontal">
            <Button
                x:Name="btn_Start"
                Clicked="btn_Start_Clicked"
                Text="Start" />
            <Button
                x:Name="btn_Stop"
                Clicked="btn_Stop_Clicked"
                Text="Stop" />
        </StackLayout>
    </StackLayout>

后面的代码:

 private void btn_Start_Clicked(object sender, EventArgs e)
    {
        btn_Start.IsVisible = false;
        stopWatch.Start();

        btn_Stop.IsVisible = true;
    }

    private void btn_Stop_Clicked(object sender, EventArgs e)
    {
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds/10);

        lbl_result.Text = elapsedTime;
        btn_Start.IsVisible = true;
        btn_Stop.IsVisible = false;
    }

更新2:

如果您想实时查看时间,可以使用 Timer。

Xaml:

<StackLayout>
    <Label x:Name="lbl_result" FontSize="Large" />
    <StackLayout Orientation="Horizontal">
        <Button
            x:Name="btn_Start"
            Clicked="btn_Start_Clicked"
            Text="Start" />
        <Button
            x:Name="btn_Stop"
            Clicked="btn_Stop_Clicked"
            Text="Stop" />
    </StackLayout>
</StackLayout>

后面的代码:

 public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
    Timer timer;
    int hours = 0, mins = 0, secs = 0, milliseconds = 0;
    private void btn_Start_Clicked(object sender, EventArgs e)
    {
        timer = new Timer();
        timer.Interval = 1; // 1 milliseconds  
        timer.Elapsed += Timer_Elapsed; ;
        timer.Start();
    }

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        milliseconds++;
        if (milliseconds >= 1000)
        {
            secs++;
            milliseconds = 0;
        }
        if (secs == 59)
        {
            mins++;
            secs = 0;

        }
        if (mins == 59)
        {
            hours++;
            mins = 0;
        }
        Device.BeginInvokeOnMainThread(() =>
        {
            lbl_result.Text = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", hours, mins, secs, milliseconds / 10);
        });
    }

    private void btn_Stop_Clicked(object sender, EventArgs e)
    {
        timer.Stop();
        timer = null;
    }
}

输出:

【讨论】:

  • 格式正确但秒表不工作,在 00:00:00.00 冻结
  • 我提供了我的示例代码。请检查我的更新。
  • 谢谢它的工作,仍然看不到实时运行的时间,如果你能帮助我看到它运行..再次感谢
  • 我已经更新了我的回复。请检查一下。
  • @Yasir 很高兴为您提供帮助。如果您的问题已经解决,请不要忘记接受答案。谢谢~
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 2011-06-20
  • 2014-10-11
  • 2012-03-20
相关资源
最近更新 更多