过去几天,我一直在尝试制作秒表。我想我只需要弄清楚如何使其真正显示在屏幕上,并每秒,毫秒等更新。我输入的任何代码都不会在屏幕上显示计时器并实际显示正在发生的事情。我将在下面发布代码以及出现的错误。任何帮助输入都将是惊人的。谢谢。
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)'
答案 0 :(得分:0)
You can try this approach. It can be done easily with an async/await method. Check this:
async void UpdateLoop()
{
while (true) //your condition when it should stop
{
label.Text = DateTime.Now.ToString("T");
await Task.Delay(1000);
}
}
Of course you don't need to display actual time, but you can keep your time in a variable.
And you can call the UpdateLoop()
method from any method/constructor. This peace of code is from the official Xamarin.Forms book (page 721). I recommended to check it out.
答案 1 :(得分:0)
我做了类似的事情,可能会对您有所帮助。基本上,我想显示启动某些硬件的时间。我在视图中有一个绑定到属性的标签。然后,我创建了一个调度程序,该调度程序每隔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");
}