【发布时间】: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