用户控件中的计时器 [英] Timer in usercontrol
问题描述
所以,我需要一个带导航的基本wpf应用程序。对于导航,我选择了UserControl。 UserControl中有TextBlock和Button,例如Home调用它。对于Timer我创建了静态类。以下是Timer.cs代码:
So, I need a basic wpf application with navigation. For navigation I chose UserControl. There is TextBlock and Button in UserControl, call it for example Home. For Timer I created static class. Here is the following Timer.cs code:
namespace WpfApplication12
{
public static class Timer
{
static DispatcherTimer dt = new DispatcherTimer();
static Stopwatch sw = new Stopwatch();
static string currenttime = string.Empty;
static Timer()
{
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
public static string Time
{
get
{
return currenttime;
}
}
private static void dt_Tick(object sender, EventArgs e)
{
if (sw.IsRunning)
{
TimeSpan ts = sw.Elapsed;
currenttime = string.Format("{0.00}:{1.00}.{2.00}",
ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
}
}
public static void StartTime()
{
sw.Start();
dt.Start();
}
public static void StopTime()
{
if (sw.IsRunning)
sw.Stop();
}
}
}
所以,问题是我试图显示定时器时间在Home.xaml(UserControl)的TextBlock上,应用程序每次都停止和关闭。
我尝试过:
这是Home.xaml(UserControl)代码:
So, the problem is when I try to display Timer time on the TextBlock in Home.xaml(UserControl), application stopping and closing every time.
What I have tried:
Here is the Home.xaml(UserControl) code:
<UserControl x:Class="WpfApplication12.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Button Content="Start" Name="btn_Start" Grid.Row="0" Click="btn_Start_Click"/>
<TextBlock Text="" Name="Display" FontSize="36" Grid.Row="1"/>
</Grid>
</UserControl>
Home.xaml.cs:
Home.xaml.cs:
namespace WpfApplication12
{
public partial class Home : UserControl
{
public Home()
{
InitializeComponent();
}
private void btn_Start_Click(object sender, RoutedEventArgs e)
{
Timer.StartTime();
Display.Text = Timer.Time;
}
}
}
MainWindow.xaml:
MainWindow.xaml:
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox>
<local:Home Width="200"/>
</ListBox>
</Grid>
</Window>
推荐答案
public partial class StopWatchDemo : Window
{
DispatcherTimer dt = new DispatcherTimer();
Stopwatch stopWatch = new Stopwatch();
string currentTime = string.Empty;
public StopWatchDemo()
{
InitializeComponent();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
void dt_Tick(object sender, EventArgs e)
{
if (stopWatch.IsRunning)
{
TimeSpan ts = stopWatch.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
ClockTextBlock.Text = currentTime;
}
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dt.Start();
}
}
}
这篇关于用户控件中的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!