<Grid x:Name="counterPanel" Background="AliceBlue">
</Grid>
代码
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
int counter = 0;
counterPanel.MouseLeftButtonDown +=
delegate(object s, MouseButtonEventArgs args)
{
if (timer.IsEnabled) timer.Stop(); else timer.Start();
};
timer.Tick +=
delegate(object s, EventArgs args)
{
counterPanel.Children.Clear();
counterPanel.Children.Add(new TextBlock
{
Text = counter++.ToString(),
Width=200,
Height=100,
FontSize=36
});
};
timer.Interval = new TimeSpan(0, 0, 1); // one second
timer.Start();
}
}