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

mpe*_*pen 19 c# wpf user-interface

我试图想出一种不显眼的方式来向用户显示小错误消息.所以我在表单中添加了状态栏,

    <StatusBar Margin="0,288,0,0" Name="statusBar" Height="23" VerticalAlignment="Bottom">
        <TextBlock Name="statusText">Ready.</TextBlock>
    </StatusBar>
Run Code Online (Sandbox Code Playgroud)

然后当他们点击"添加"按钮时,它应该做一些事情,或者显示错误信息:

private void DownloadButton_Click(object sender, RoutedEventArgs e)
{
    addressBar.Focus();
    var url = addressBar.Text.Trim();
    if (string.IsNullOrEmpty(url))
    {
        statusText.Text = "Nothing to add.";
        return;
    }
    if (!url.Contains('.'))
    {
        statusText.Text = "Invalid URL format.";
        return;
    }
    if (!Regex.IsMatch(url, @"^\w://")) url = "http://" + url;
    addressBar.Text = "";
Run Code Online (Sandbox Code Playgroud)

但是这个消息只是存在于应用程序的生命周期......我想我应该在大约5秒后重置它...我怎么能设置一个计时器来做到这一点?

额外奖励:我如何给它一个漂亮的淡出效果呢?


我创造了一个System.Timers.Timer,

    private Timer _resetStatusTimer = new Timer(5000);

    void _resetStatusTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        statusText.Text = "Ready";
    }
Run Code Online (Sandbox Code Playgroud)

但是Elapsed事件运行在与UI不同的线程上,它不喜欢...如何解决这个问题?

as-*_*cii 37

您可以使用Storyboard来完成这项工作.

<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>
Run Code Online (Sandbox Code Playgroud)

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

<Window.Triggers>
    <EventTrigger RoutedEvent="TextBoxBase.TextChanged" SourceName="textBox">
        <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
    </EventTrigger>
</Window.Triggers>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

然后在这种情况下创建一个名为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(""));
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Aph*_*hex 8

您的计时器是一种很好的方法,您甚至可以确定您的问题:您只需要一种statusText.Text在UI线程上访问的方法.(在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();
Run Code Online (Sandbox Code Playgroud)