c# - StatusBar 消息实现超时

标签 c# wpf

我想实现一个状态栏,我将能够在指定的时间段内显示消息,之后它们将淡出。

这可以用 wpf 中任何现成的控件来完成吗?我在 StatusBar 中找不到这样的功能,据我所知,它是其他项目的容器。

有什么建议吗?

最佳答案

StatusBar 确实只是其他项目的容器。
我认为没有这样的内置功能。<​​/p>

但是你可以使用 Timer实现你想要的。

创建一个将文本消息设置为 StatusBarItem 内容的方法,在 TimerTimer.Elapsed 中设置时间> 您从 StatusBarItem 中删除文本的事件。

状态栏 XAML:

<StatusBar Height="25" Margin="5">
    <StatusBarItem x:Name="StatusMessage" />
</StatusBar>

程序代码:

private void ShowStatusMessage(string message)
{
    StatusMessage.Content = message;
    var timer = new System.Timers.Timer();
    timer.Interval = 2000; //2 seconds
    timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
    {
        //stop the timer
        timer.Stop();
        //remove the StatusMessage text using a dispatcher, because timer operates in another thread
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            StatusMessage.Content = "";
        }));
    };
    timer.Start();
}

如果你想要动画你应该寻找DoubleAnimation

关于c# - StatusBar 消息实现超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383494/

相关文章:

c# - VS2005 : when stepping through C# code, 有没有办法跳过代码段?

c# - 如何让 ffmpeg 在 try/catch 上为工作目录和参数抛出异常?

wpf - 无法将类型 'MS.Internal.NamedObject' 的对象转换为自定义类型

c# - WPF MVVM DataBindings 停止更新

c# - 如何将文件从我的应用程序拖到桌面?

c# - 有什么办法可以在 div 中放置一个链接

c# - 面板固定位置

c# - wpf中的ViewModel层次结构

WPF 编程方法论

wpf - 所有 DataGrid 的 DataGridTextColumn.EditingElementStyle