示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

一、目的:不同交互场景需要提示不同的消息,不同的消息需要用不同的效果来展示,应用DialogHost(对话框)、NotifyIcon(消息提示)、Snackbar(气泡消息)显示各种场景提示消息,应用在ViewModel中

 

 

 

二、实现:

1、等待对话框

2、确定对话框

3、确定与取消对话框

4、百分比进度和文本进度对话框

5、气泡提示消息(NotifyIcon)

6、提示消息(Snackbar)

三、示例:

说明:

1、对话框:常规对话消息如上图,等待对话框、消息对话、进度对话框;

(目前只封装如上这几种,自定义对话框只需创建用户控件调用通用加载方法即可,后续更新...)

2、提示消息:当进度保存成功是需要一个提示消息,显示2s自动隐藏即可(如图中友情提示部分分) ;

3、气泡消息:当程序处于隐藏或某种状态时需要应用气泡提示消息;

四、代码:

    [ViewModel("Loyout")]
    class LoyoutViewModel : MvcViewModelBase
    {

       
        /// <summary> 命令通用方法 </summary>
        protected override async void RelayMethod(object obj)

        {
            string command = obj?.ToString();

            //  Do:对话消息
            if (command == "Button.ShowDialogMessage")
            {
                await MessageService.ShowSumitMessge("这是消息对话框?");

            }
            //  Do:等待消息
            else if (command == "Button.ShowWaittingMessge")
            {

                await MessageService.ShowWaittingMessge(() => Thread.Sleep(2000));

            }
            //  Do:百分比进度对话框
            else if (command == "Button.ShowPercentProgress")
            {
                Action<IPercentProgress> action = l =>
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            l.Value = i;

                            Thread.Sleep(50);
                        }

                        Thread.Sleep(1000);

                        MessageService.ShowSnackMessageWithNotice("加载完成!");
                    };
                await MessageService.ShowPercentProgress(action);

            }
            //  Do:文本进度对话框
            else if (command == "Button.ShowStringProgress")
            {
                Action<IStringProgress> action = l =>
                {
                    for (int i = 1; i <= 100; i++)
                    {
                        l.MessageStr = $"正在提交当前页第{i}份数据,共100份";

                        Thread.Sleep(50);
                    }

                    Thread.Sleep(1000);

                    MessageService.ShowSnackMessageWithNotice("提交完成:成功100条,失败0条!");
                };

                await MessageService.ShowStringProgress(action);

            }
            //  Do:确认取消对话框
            else if (command == "Button.ShowResultMessge")
            {
                Action<object, DialogClosingEventArgs> action = (l, k) =>
                {
                    if ((bool)k.Parameter)
                    {
                        MessageService.ShowSnackMessageWithNotice("你点击了取消");
                    }
                    else
                    {
                        MessageService.ShowSnackMessageWithNotice("你点击了确定");
                    }
                };

                MessageService.ShowResultMessge("确认要退出系统?", action);


            }
            //  Do:提示消息
            else if (command == "Button.ShowSnackMessage")
            {
                MessageService.ShowSnackMessageWithNotice("这是提示消息?");
            } 
            //  Do:气泡消息
            else if (command == "Button.ShowNotifyMessage")
            {
                MessageService.ShowNotifyMessage("你有一条报警信息需要处理,请检查", "Notify By HeBianGu");
            }
        }
    }

下载地址:https://github.com/HeBianGu/WPF-ControlBase.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章