Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
silverlight的秒表?_Silverlight - Fatal编程技术网

silverlight的秒表?

silverlight的秒表?,silverlight,Silverlight,Silverlight没有秒表 你会用什么来代替它 我看到一些帖子说,要创建一个空动画并从Storyboard类调用GetCurrentTime(),我无法让它工作 你会怎么做?用这个你可以做一些事情。首先是使用类似于环境的东西。然而,我认为更好的办法是利用一个 要将Dispatcher设置为像秒表一样工作,我们还需要一个表示它运行时间的关联TimeSpan。我们可以实例化Dispatchermer并设置它的时间间隔,以及Tick事件的处理程序 DispatcherTimer _timer; T

Silverlight没有秒表

你会用什么来代替它

我看到一些帖子说,要创建一个空动画并从Storyboard类调用GetCurrentTime(),我无法让它工作


你会怎么做?

用这个你可以做一些事情。首先是使用类似于环境的东西。然而,我认为更好的办法是利用一个

要将Dispatcher设置为像秒表一样工作,我们还需要一个表示它运行时间的关联TimeSpan。我们可以实例化Dispatchermer并设置它的时间间隔,以及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>
请看我的表。源代码是。关于它的两篇文章是和。Silverlight动画模型非常适合秒表


这是一个可以添加到项目中的替换类。

这就是我所做的。这很简单,对我来说效果很好:

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));

您所需要做的就是在开始目标任务之前保留一个长变量来保存总刻度。

很有趣,因为这个页面似乎表明它存在-@James这确实很有趣。要么该页面只是错误地出现在那里,要么它将进入SL5。这篇文章建议您可以引用WP7DLL并使用Stopwatch类。不过我还没试过@James,@Oskar:Silverlight的秒表确实存在,但仅适用于XNA框架。请参阅链接的版本信息部分,并将其与System之类的内容进行比较。Boolean:感谢您的共享,伟大的实现:DI尝试使用相同的方法,但结果不正确。可能是由于功能开销或硬件原因,应用程序中的时间比实际的挂钟慢。你能帮我解决这个问题吗?我不知道这会给问题增加多少,但很好!使用
DateTime.UtcNow
,除非您真正关心时区等。
<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));
}
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));