2

我的 XAML 中有一个包含 ProgressRing 的 Grid,当应用程序执行长时间运行的任务时,它会淡入视图:

        <Grid Height="100" Width="100"  >
            <Grid Background="#55000000" />
            <ProgressRing x:Name="mapProgressRing"  Width="50" Height="50" Foreground="White" />    
        </Grid>
        </Border>

但是,我想将动画延迟 1 秒,其想法是如果任务在不到一秒的时间内完成,则永远不会显示 Grid。从而避免在执行非常短的任务时网格不断闪烁。

然而,尽管在上面的 XAML 中设置了 BeginTime,网格总是立即开始淡入视图,即使我在代码中尝试这样做也是如此。这是我的代码:

    void dataSou_WillStartFetchingSomething(object sender, EventArgs e)
    {
        // Don't bother fading in spinner unless it takes more than a second
        showSpinner(TimeSpan.FromSeconds(1));
    }
    void Instance_WillEndFetchingSomething(object sender, EventArgs e)
    {
        hideSpinner();
    }

    #region Spinner
    void showSpinner(TimeSpan? delayStartTime)
    {
        // Stop animation in case it's already running
        mapRingFadeOutStoryBoard.Stop();
        // Fade in, delaying the start by the specified time
        mapRingFadeInStoryBoard.BeginTime = delayStartTime;
        mapRingFadeInStoryBoard.Begin();
    }
    void hideSpinner()
    {
        // Stop the 'fade in' animation in case it's still running
        mapRingFadeInStoryBoard.Stop();

        // Fade out
        mapRingFadeOutStoryBoard.Begin();
    }
    #endregion

我究竟做错了什么?

4

1 回答 1

0

Not sure what's wrong, but you could use async/await instead. Just mark your method as async and then do "await Task.Delay(1000)" to wait for a second before starting the animation.

于 2012-08-14T15:52:28.133 回答