Silverlight沒有StopWatch。silverlight秒錶?
你會用什麼來代替它?
我看到了有關人說,創建一個空的動畫和故事板類調用GetCurrentTime(),我無法得到它的工作一些職位...
你會怎麼做?
Silverlight沒有StopWatch。silverlight秒錶?
你會用什麼來代替它?
我看到了有關人說,創建一個空的動畫和故事板類調用GetCurrentTime(),我無法得到它的工作一些職位...
你會怎麼做?
你可以用這個做幾件事。拳頭使用像Environment.TickCount之類的東西,例如here。但是,我認爲可能更好的一點是使用DispatcherTimer。
要設置DispatcherTimer使其像秒錶一樣工作,我們還需要一個表示其運行時間的關聯TimeSpan。我們可以實例化DispatcherTimer並設置它的時間間隔以及Tick事件的處理程序。
DispatcherTimer _timer;
TimeSpan _time;
public Page()
{
InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
_timer.Tick += new EventHandler(OnTimerTick);
}
在我們可以創建一些簡單的啓動和停止計時器我們,以及顯示秒錶數據的UI:
<StackPanel>
<Button Content="Start" x:Name="uiStart" Click="OnStartClick" />
<Button Content="Stop" x:Name="uiStop" Click="OnStopClick" />
<TextBlock x:Name="uiDisplay"/>
</StackPanel>
現在,所有剩下的是事件處理程序。
OnTimerTick處理程序將遞增並顯示我們的秒錶數據。
我們的開始處理程序將負責初始化/重新生成我們的TimeSpan,而停止處理程序將停止DispatcherTimer。
void OnTimerTick(object sender, EventArgs e)
{
_time = _time.Add(new TimeSpan(0, 0, 0, 0, 10));
display.Text = _time.ToString();
}
private void OnStartClick(object sender, RoutedEventArgs e)
{
_time = new TimeSpan(0,0,0,0,0);
_timer.Start();
}
private void OnStopClick(object sender, RoutedEventArgs e)
{
_timer.Stop();
}
我嘗試使用相同的方法,但結果不正確。也許由於功能開銷或硬件原因,應用程序中的時間比實際的掛鐘慢。你能幫助解決這個問題嗎? – letsc 2012-01-05 10:45:11
請看我的手錶here。源代碼是here。關於它的兩篇文章是here和here。 Silverlight動畫模型非常適合秒錶。
alt text http://xmldocs.net/ball2/background.png
<Storyboard x:Name="Run" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="SecondHand" x:Name="SecondAnimation"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:01:00" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="MinuteHand"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="01:00:00" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="HourHand"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="12:00:00" Value="360"/>
,並運行該故事板的代碼是在這裏:
private void RunWatch()
{
var time = DateTime.Now;
Run.Begin();
Run.Seek(new TimeSpan(time.Hour, time.Minute, time.Second));
}
非常有趣!哈哈 – 2010-12-07 19:09:57
這裏是一個Stopwatch替換類,您可以添加到您的項目。
這就是我所做的。它的簡單,工作對我非常好:
long before = DateTime.Now.Ticks;
DoTheTaskThatNeedsMeasurement();
long after = DateTime.Now.Ticks;
TimeSpan elapsedTime = new TimeSpan(after - before);
MessageBox.Show(string.Format("Task took {0} milliseconds",
elapsedTime.TotalMilliseconds));
所有你需要做的就是保持一個長變量開始的目標任務之前,持有總蜱。
有趣,因爲這個網頁似乎表明它存在 - http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.startnew(v=VS.95).aspx – 2011-04-14 19:06:52
@詹姆斯這是的確非常有趣。無論是那個頁面出現錯誤,或者它可能會來到SL5。這篇文章建議你可以引用WP7 dll並使用Stopwatch類。我還沒有嘗試過。 http://forums.silverlight.net/forums/p/207108/486425.aspx – Oskar 2011-05-10 10:56:10
@James,@Oskar:Silverlight的秒錶確實存在,但僅限於XNA Framework。查看鏈接的「版本信息」部分,並將其與System.Boolean類似的內容進行比較:http://msdn.microsoft.com/zh-cn/library/system。boolean%28v = VS.95%29.aspx – 2011-05-22 07:57:35