WPF快速通知控件 [英] WPF quick notification control

查看:245
本文介绍了WPF快速通知控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVVM开发WPF应用程序时,我碰到了一个点,我在可编辑数据网格上有一个保存按钮,该按钮在后台进行处理,但实际上并未更新UI中的任何内容,因此用户可以

Developing a WPF application using MVVM, I have hit a point where I've got a "save" button on an editable datagrid which does behind the scenes processing but doesn't actually update anything in the UI, so the user has no way of knowing that the save has been successful.

我对WPF还是很陌生,我想这里会有一个简单的Flash消息控件,可以用来通知用户成功,然后逐渐消失,而无需执行任何操作。但是,似乎在香草WPF中没有什么可以做到的,而且似乎也没有很多定制解决方案。

I'm pretty new to WPF, and I presumed there would be a simple flash message control that one could use to notify the user of success and then faded away without them having to do anything. But it seems there's nothing in vanilla WPF that can do this, and there don't seem to be a lot of custom solutions either.

我不想使用任何类型的消息框,因为它会迫使用户采取措施来消除警报-我需要能够在不干扰其工作流程的情况下简短地闪烁消息的内容。我在寻找类似JavaScript Toastr库的东西-

I don't want to use any kind of messagebox because it forces the user to take an action to dismiss the alert - I need something that breifly flashes a message without interfering with their workflow. I'm after something a bit like the JavaScript Toastr library -

http://codeseven.github.io/toastr/demo.html

有人可以为此指出我的现有控件吗? ,或者我可能会从哪里开始?

can anyone either point me at an existing control for this, or where I might start at building one?

推荐答案

我认为您不需要任何第三方控件。您总是可以创建一个自定义控件,将其粘贴到布局中,然后将布局的 Visibility 属性绑定到视图模型。另一种选择是使用 StatusBar 来通知客户端,例如在Word或VisualStudio中。有一个简单的例子:

I think you don't need any third party controls. You always may create a custom control, paste it in a layout and bind layouts Visibility property to your view model. Another option is to use StatusBar to notify clients like in Word or VisualStudio. There is a just brief example:

在xaml中的某个地方:

Somewhere in xaml:

// ..
<StatusBar DockPanel.Dock="Bottom">
    <StatusBarItem>
        <Label Content="{Binding Message}"></Label>
    </StatusBarItem>
</StatusBar>
// ..

您代码中的某个位置(我喜欢将async / await与WPF):

Somewhere in your code (I like to use async/await with WPF):

// ..
statusBarViewModel.Message = "Processing the file..."; // assumed that you bind this view model to the view
await DoWork(); // do much work
await statusBarViewModel.ShowMessageAndHide("File saved"); // show final message and hide it after some time
// ..

然后 StatusBarViewModel

public class StatusBarViewModel : INotifyPropertyChanged
{

    private string message = string.Empty;

    public string Message
    {
        get { return message; }
        set
        {
            if (value == message) return;
            message = value;
            OnPropertyChanged();
        }
    }

    public async Task ShowMessageAndHide(string message)
    {
        Message = message;
        await Task.Delay(5000);
        Message = string.Empty;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

这篇关于WPF快速通知控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆