等待.1秒直到隐藏图像

时间:2011-04-09 17:21:52

标签: c# visual-studio visual-studio-2010 windows-phone-7 wait

我正在使用C#为Windows Phone制作一个小程序。它应该做的一件事是当用户点击“隐藏”按钮时隐藏按钮工具栏。

我已经完成了隐藏工具栏的代码。它隐藏了按钮,就像预期的那样。但现在发生的是所有按钮立即消失。为了制作一种“动画”,我决定等待.1秒,直到隐藏所有按钮。

我怎么等.1秒?

这是我现在的代码。

    bool panelopened = false;

    private void image1_MouseEnter(object sender, MouseEventArgs e)
    {
        if (panelopened == false)
        {
            ImageSourceConverter imgs = new ImageSourceConverter();
            image1.SetValue(Image.SourceProperty, imgs.ConvertFromString("/Main%20View;component/Images/hide.png"));
            image3.Width = 50;
            image4.Width = 50;
            image5.Width = 50;
            panelopened = true;
        }
        else
        {
            ImageSourceConverter imgs = new ImageSourceConverter();
            image1.SetValue(Image.SourceProperty, imgs.ConvertFromString("/Main%20View;component/Images/more.png"));
            image3.Width = 0;
            image4.Width = 0;
            image5.Width = 0;
            panelopened = false;
        }
    } 

2 个答案:

答案 0 :(得分:5)

查看此previous answer。使用它你可以做

Dispatcher.DelayInvoke(TimeSpan.FromSeconds(0.1), () => 
{
   image3.Width = 0;
   image4.Width = 0;
   image5.Width = 0;
}

答案 1 :(得分:1)

你这样做的方式不是最好的 - 在UI Thread上做很多工作。

我在我的应用程序中使用以下代码。 Remeber,Sroryboards动画在Compositor Thread上运行,它是轻量级的,并且专门用于此目的。

// fade animation of the Popup to opacity 1.0
    private void ShowPopup()
    {
        exitPopup.Visibility = Visibility.Visible;
        Storyboard storyboard = new Storyboard();
        DoubleAnimation fadeAnimation = new DoubleAnimation();
        fadeAnimation.To = 1;
        fadeAnimation.Duration = TimeSpan.FromSeconds(1);
        //fadeAnimation.FillBehavior = FillBehavior.Stop;
        StoryBoardHelper.SetTarget(fadeAnimation, exitPopup);
        Storyboard.SetTargetProperty(fadeAnimation, new PropertyPath("(Canvas.Opacity)"));
        storyboard.Children.Add(fadeAnimation);
        storyboard.Duration = fadeAnimation.Duration;
        storyboard.Begin();
    }

    // fade aninmation to opacity 0.0
    private void ClosePopup()
    {
        Storyboard storyboard = new Storyboard();
        DoubleAnimation fadeAnimation = new DoubleAnimation();
        fadeAnimation.To = 0;
        fadeAnimation.Duration = TimeSpan.FromSeconds(0.2);
        //fadeAnimation.FillBehavior = FillBehavior.Stop;
        StoryBoardHelper.SetTarget(fadeAnimation, exitPopup);
        Storyboard.SetTargetProperty(fadeAnimation, new PropertyPath("(Canvas.Opacity)"));
        storyboard.Children.Add(fadeAnimation);
        storyboard.Duration = fadeAnimation.Duration;
        storyboard.Begin();
        storyboard.Completed += (sender, e) => exitPopup.Visibility = Visibility.Collapsed;
    }

你还需要一件事。设置BeginTime以启动动画形式1s。

您始终可以将此代码更改为更小且更明确的XAML。