简体   繁体   English

Xamarin Forms 秒表毫秒格式

[英]Xamarin Forms Stopwatch Milliseconds Format

I'm using simple stopwatch in xamarin forms, everything is ok but when I use lable to view it, it shows the milliseconds include (7) digits as follows: 00:00:00:0000000我在 xamarin forms 中使用简单的秒表,一切正常,但是当我使用标签查看它时,它显示毫秒包括(7)位数字如下:00:00:00:0000000

how i can make it less or formatted it as I want?我怎样才能减少它或根据需要格式化它?

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


            });

You could format with string.Format .您可以使用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);

在此处输入图像描述

Update:更新:

Xaml: 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>

Code behind:后面的代码:

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

Update2:更新2:

If you wanna see the time running as real time, you could use Timer.如果您想实时查看时间,可以使用 Timer。

Xaml: 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>

Code behind:后面的代码:

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

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM