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

48 篇文章 11 订阅

一、目的:不同交互场景需要提示不同的消息,不同的消息需要用不同的效果来展示,应用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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自定义显示 DialogHost,你可以按照以下步骤进行操作: 1. 创建 DialogHost 控件的自定义模板。 2. 在模板,添加一个 ContentPresenter 控件,用来显示 DialogHost 的内容。 3. 在模板,添加一个 Grid 控件,用来放置 DialogHost 的按钮和标题等控件。 4. 在 Grid 添加需要的控件,比如标题、关闭按钮等。 5. 在关闭按钮的 Click 事件,使用 VisualTreeHelper 查找 DialogHost 控件,并将其 IsOpen 属性设置为 false,关闭对话框。 6. 在需要显示对话框的地方,使用 DialogHost.Show 方法显示 DialogHost 控件,并将要显示的内容作为参数传入。 示例代码如下: ```xml <Style TargetType="{x:Type MaterialDesignThemes:DialogHost}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type MaterialDesignThemes:DialogHost}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Border Grid.Row="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter Content="{TemplateBinding DialogContent}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <Grid Grid.Row="0" Background="{TemplateBinding Background}" Height="30"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{TemplateBinding Title}" VerticalAlignment="Center" Margin="10,0,0,0"/> <Button Grid.Column="1" Content="X" Width="20" Height="20" FontSize="12" FontFamily="Segoe MDL2 Assets" Background="Transparent" Foreground="{DynamicResource MaterialDesignDivider}" Command="{Binding Path=CloseCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MaterialDesignThemes:DialogHost}}}" Cursor="Hand" ToolTip="Close"/> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 使用 DialogHost.Show 方法显示对话框: ```csharp var dialog = new CustomDialog(); DialogHost.Show(dialog, "RootDialog"); ``` 其,CustomDialog自定义对话框的内容。 "RootDialog" 是 DialogHost 控件的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值