using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
namespace Silverlight20.Tip
{
public partial
class LoopsDemo : UserControl
{
public LoopsDemo()
{
InitializeComponent();
this.Loaded +=
new RoutedEventHandler(LoopsDemo_Loaded);
}
void LoopsDemo_Loaded(
object sender, RoutedEventArgs e)
{
DispatcherTimerDemo();
StoryboardDemo();
TimerDemo();
CompositionTargetDemo();
}
/// <summary> /// DispatcherTimer - 在 UI 线程上循环(会受到 UI 线程的影响) /// </summary> private void DispatcherTimerDemo()
{
DispatcherTimer dTimer =
new DispatcherTimer();
dTimer.Interval = TimeSpan.Zero;
dTimer.Tick +=
new EventHandler(dTimer_Tick);
dTimer.Start();
}
void dTimer_Tick(
object sender, EventArgs e)
{
resultDispatcherTimer.Text = DateTime.Now.ToString(
"hh:mm:ss fff");
}
Storyboard _board;
/// <summary> /// Storyboard - 在非 UI 线程上循环 /// </summary> private void StoryboardDemo()
{
_board =
new Storyboard();
_board.Duration = TimeSpan.Zero;
_board.Completed +=
new EventHandler(_board_Completed);
_board.Begin();
}
void _board_Completed(
object sender, EventArgs e)
{
resultStoryBoard.Text = DateTime.Now.ToString(
"hh:mm:ss fff");
_board.Begin();
}
Timer _timer;
/// <summary> /// Timer - 在非 UI 线程上循环 /// </summary> private void TimerDemo()
{
_timer =
new Timer(_timer_CallBack,
null, TimeSpan.Zero, TimeSpan.Zero);
}
private void _timer_CallBack(
object state)
{
this.Dispatcher.BeginInvoke(() =>
{
resultTimer.Text = DateTime.Now.ToString(
"hh:mm:ss fff");
});
_timer.Change(TimeSpan.Zero, TimeSpan.Zero);
}
/// <summary> /// CompositionTarget.Rendering - 每呈现 1 帧都会触发此事件(相当于 Flash 的 Event.ENTER_FRAME) /// </summary> private void CompositionTargetDemo()
{
CompositionTarget.Rendering +=
new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(
object sender, EventArgs e)
{
resultCompositionTarget.Text = DateTime.Now.ToString(
"hh:mm:ss fff");
}
}
}