【问题标题】:Display Stopwatch to screen?在屏幕上显示秒表?
【发布时间】:2018-12-03 12:31:36
【问题描述】:

过去几天我一直在尝试制作秒表。我想我只需要弄清楚如何真正让它显示在屏幕上并每秒更新一次,毫秒等。我输入的任何代码都不会在屏幕上显示计时器并实际显示正在发生的事情。我将发布下面的代码和我遇到的错误。任何帮助输入都会很棒。谢谢。

 public partial class MainPage : ContentPage
{

    private const string Format = "{0:00}:{1:00}:{2:00}.{3:00}";

    DispatcherTimer dTimer = new DispatcherTimer();
    Stopwatch stopWatch = new Stopwatch();
    string currentTime = string.Empty;


    public MainPage()
    { 

        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan tSpan = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string ClockTextBlock = String.Format(Format,
            tSpan.Hours, tSpan.Minutes, tSpan.Seconds,
            tSpan.Milliseconds / 10);
        Console.WriteLine("RunTime " + ClockTextBlock);
        InitializeComponent();

        InitializeComponent();

        dTimer.Tick += new EventHandler(dt_Tick);
        dTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);

        void dt_Tick(object sender, EventArgs e)
        {
            if (stopWatch.IsRunning)
            {
                TimeSpan t = stopWatch.Elapsed;
                currentTime = String.Format("{0:00}:{1:00}:{2:00}",
                    tSpan.Hours, tSpan.Minutes, tSpan.Seconds);
                ClockTextBlock.ToString (currentTime);
            }
        }


    }

    public String Display(Object sender, String ClockTextBlock)
    {

        return ClockTextBlock + String.Format(Format);
    }
    public void StartButton_Click(Object sender, Stopwatch stopwatch)
    {
        dTimer.Start();
    }

    public void StopButton_Click(Object sender, Stopwatch stopwatch)
    {
        dTimer.Stop();
    }
    public void ResetButton_Click(Object sender, Stopwatch stopwatch)
    {
        stopwatch.Reset();
    }




}

}

错误:

无法从“字符串”转换为“System.IFormatProvider”代码
语言不支持 DispatcherTimer.Tick';直接试试 调用访问器方法 'DispatcherTimer.add_Tick(EventHandler)' 或 'DispatcherTimer.remove_Tick(EventRegistrationToken)'

【问题讨论】:

  • 这有什么问题,你有什么错误吗?如果是这样,请编辑您的问题并列出它们
  • 您的秒表在您致电stopwatch.Stop();后保持停止状态
  • 你有两个变量stopWatch。你可能只想要一个。
  • 刚刚发布了错误。
  • 是的,你是对的,安德鲁,抱歉,拖了这么久。

标签: c# xamarin xamarin.forms stopwatch


【解决方案1】:

您可以尝试这种方法。可以使用async/await 方法轻松完成。检查这个:

async void UpdateLoop()
{
    while (true) //your condition when it should stop
    {
        label.Text = DateTime.Now.ToString("T");
        await Task.Delay(1000);
    }
}

当然,您不需要显示实际时间,但您可以将时间保存在变量中。 您可以从任何方法/构造函数调用UpdateLoop() 方法。这种代码和平来自the official Xamarin.Forms book(第 721 页)。我建议检查一下。

【讨论】:

    【解决方案2】:

    我做了类似的事情,可能会对你有所帮助。基本上我想显示一些硬件已经启动的时间长度。我在视图中有一个标签,它绑定到一个属性。然后我创建了一个调度程序,它每 50 毫秒调用一个方法来更新属性。在下面的示例中,它在调用 View-Model 的构造函数时开始。

    XAML

    <Label Grid.Row="5" Grid.Column="2" Content="{Binding TimePortModuleIsUp}" />
    

    视图模型

     private string timePortModuleIsUp;
    
     /// <summary>
     /// Gets / Sets how long the port module has been booted for.
     /// </summary>
     public string TimePortModuleIsUp
     {
      get
      {
           return timePortModuleIsUp;
      }
      Set
      {
           timePortModuleIsUp = value;
           OnPropertyChanged("TimePortModuleIsUp");
      }
     }
    

    创建一个 DispatcherTimer 对象。

    private DispatcherTimer t;
    

    视图模型中的构造函数

    这将创建一个调度程序对象,该对象每 50 毫秒调用一次 t_tick 并启动它。

    // Create a dispatcher that calls t_tick every 50 ms.
    t = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 50), DispatcherPriority.Background, t_Tick, Dispatcher.CurrentDispatcher);
    
    // Start the timer.
    t.IsEnabled = true;
    

    最后是每 50 毫秒调用一次的方法,用于更新标签绑定的属性。

        /// <summary>
        /// Method that updates a property representing how long the port module has been booted for.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_Tick(object sender, EventArgs e)
        {
            this.TimePortModuleIsUp = new DateTime((DateTime.Now - this.ModuleBootTime).Ticks).ToString("HH:mm:ss");
        }
    

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多