WPF在X秒后淡出状态栏文本?

时间:2010-10-19 17:25:06

标签: c# wpf user-interface

3 个答案:

答案 0 :(得分:37)

您可以使用故事板来实现这一目标。

<Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="statusBarItem">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

当必须显示消息时,您只需以编程方式调用StoryBoard的Begin方法或插入触发器,如下所示。

<Window.Triggers>
    <EventTrigger RoutedEvent="TextBoxBase.TextChanged" SourceName="textBox">
        <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
    </EventTrigger>
</Window.Triggers>

编辑:另一种选择是这样做:

<TextBlock Name="statusText" Text="{Binding Path=StatusBarText, NotifyOnTargetUpdated=True}">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="1"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>

然后在这种情况下创建一个名为StatusBarText的DependencyProperty,实现如下:

public string StatusBarText
    {
        get { return (string)GetValue(StatusBarTextProperty); }
        set { SetValue(StatusBarTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StatusBarText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StatusBarTextProperty =
        DependencyProperty.Register("StatusBarText", typeof(string), typeof(MyOwnerClass), new UIPropertyMetadata(""));

希望这有帮助。

答案 1 :(得分:8)

您的计时器是一种很好的方法,您甚至可以确定您的问题:您只需要一种方法来访问UI线程上的statusText.Text。 (在WPF中,禁止UI线程以外的线程访问UI元素)。 WPF调度员来救援:

http://msdn.microsoft.com/en-us/magazine/cc163328.aspx#S5

您可以使用DispatcherTimer类完成您尝试的操作(这是他们的代码):

// Create a Timer with a Normal Priority
_timer = new DispatcherTimer();

// Set the Interval to 2 seconds
_timer.Interval = TimeSpan.FromMilliseconds(2000); 

// Set the callback to just show the time ticking away
// NOTE: We are using a control so this has to run on 
// the UI thread
_timer.Tick += new EventHandler(delegate(object s, EventArgs a) 
{ 
    statusText.Text = string.Format(
        "Timer Ticked:  {0}ms", Environment.TickCount); 
});

// Start the timer
_timer.Start();

答案 2 :(得分:1)

await Task.Delay(5000); // Wait 5 seconds

for (int i = 99; i >= 0; i--)
{
    statusText.Opacity = i / 100d;

    await Task.Delay(15); // The animation will take 1.5 seconds
}

statusText.Visibility = statusText.Hidden;

statusText.Opacity = 1;

如果你想再次显示statusText,你必须将它的Visibility设置为Visible

statusText.Visibility = Visibility.Visible;

还要确保将 async 修饰符添加到您的方法签名中,否则编译器会向您显示与 await 运算符相关的错误。 await 用于在文本淡出时 UI 仍然响应。