gpt4 book ai didi

c# - 在 TextBlock 中运行秒表

转载 作者:行者123 更新时间:2023-11-30 14:38:12 25 4
gpt4 key购买 nike

我正在尝试获取一个文本 block 来显示秒表的运行时间,但不确定如何实现。

我已经尝试使用绑定(bind)到秒表的 elapsed 方法,但这不会显示任何内容。

<TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,0,398,0" Name="textBlockElapsed" Text="{Binding Path=watch.Elapsed, Mode=OneWay}" VerticalAlignment="Top" />

Stopwatch watch = new Stopwatch();

最佳答案

尝试以下示例代码:MainWindow.xaml.cs

public partial class MainWindow : Window
{

DispatcherTimer dt = new DispatcherTimer();
Stopwatch stopWatch = new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}

void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (Environment.HasShutdownStarted)
{
MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text);
}

}

void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
// ...
case SessionSwitchReason.SessionLock:
if (stopWatch.IsRunning)
stopWatch.Stop();
break;
case SessionSwitchReason.SessionUnlock:
stopWatch.Start();
dt.Start();
break;
case SessionSwitchReason.SessionLogoff:
MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text);
break;
// ...
}
}

void dt_Tick(object sender, EventArgs e)
{
if (stopWatch.IsRunning)
{
TimeSpan ts = stopWatch.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Hours, ts.Minutes, ts.Seconds);
ClockTextBlock.Text = currentTime;
}
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dt.Start();
}

private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
stopWatch.Stop();
}

private void ResetButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Reset();
stopWatch.Start();
}
}

主窗口.xaml:

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.3*"/>
<RowDefinition Height="0.2*"/>

</Grid.RowDefinitions>
<TextBlock Name="ClockTextBlock"
TextAlignment="Center"
VerticalAlignment="Center"
FontSize="35" Foreground="Red"
Grid.ColumnSpan="4"
Grid.Row="0" />
<Button Content="Start"
Name="StartButton"
Grid.Row="1"
Grid.Column="0"
Width="60" Height="35"
Click="StartButton_Click" />
<Button Content="Stop"
Name="StopButton"
Grid.Row="1"
Grid.Column="1"
Width="60" Height="35"
Click="StopButton_Click" />
<Button Content="Reset"
Name="ResetButton"
Grid.Row="1"
Grid.Column="3"
Width="60" Height="35"
Click="ResetButton_Click" />
</Grid>

关于c# - 在 TextBlock 中运行秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com