You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否创建点击后不从操作中心消失的Windows通知吐司按钮操作?

Windows通知吐司按钮点击后保留在操作中心的实现方案

当然可以做到!在Windows的通知吐司里,要实现点击按钮后通知不从操作中心消失,核心就是别在按钮的点击逻辑里主动关闭通知,再配合一些通知行为的设置就能搞定。下面是具体的实现思路和代码示例:

核心思路

默认情况下,当用户点击吐司按钮时,系统会自动将通知从操作中心移除。要改变这个行为,我们需要:

  • 为通知设置永不过期(或超长有效期),避免系统自动清理
  • 在按钮点击事件处理器中,不执行通知关闭操作,甚至可以选择更新通知内容

C# 代码示例(基于WinRT API)

using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
using Microsoft.Toolkit.Uwp.Notifications; // 推荐使用这个工具库简化开发

// 构建吐司通知内容
ToastContent content = new ToastContent()
{
    Duration = ToastDuration.Long,
    Visual = new ToastVisual()
    {
        BindingGeneric = new ToastBindingGeneric()
        {
            Children =
            {
                new AdaptiveText() { Text = "可保留的测试通知" },
                new AdaptiveText() { Text = "点击「保留通知」按钮,我会留在操作中心~" }
            }
        }
    },
    Actions = new ToastActionsCustom()
    {
        Buttons =
        {
            new ToastButton("保留通知", "action=keep")
            {
                ActivationType = ToastActivationType.Foreground
            },
            new ToastButton("关闭通知", "action=close")
            {
                ActivationType = ToastActivationType.Foreground
            }
        }
    }
};

// 创建通知实例
ToastNotification toast = new ToastNotification(content.GetXml());
// 设置通知永不过期(系统不会自动移除)
toast.ExpirationTime = null;
// 给通知设置唯一标签,方便后续管理
toast.Tag = "persistent-toast";

// 监听通知激活事件(按钮点击触发)
ToastNotificationManagerCompat.OnActivated += async args =>
{
    // 获取按钮传递的参数
    string actionArgs = args.Argument;

    if (actionArgs == "action=keep")
    {
        // 这里不执行关闭操作,通知会继续留在操作中心
        // 可选:更新通知内容,反馈用户操作
        UpdatePersistentToast();
    }
    else if (actionArgs == "action=close")
    {
        // 仅在用户点击关闭按钮时,主动从操作中心移除通知
        ToastNotificationManagerCompat.History.Remove("persistent-toast");
    }
};

// 显示通知
ToastNotificationManagerCompat.CreateToastNotifier().Show(toast);

// 可选:更新通知的辅助方法
void UpdatePersistentToast()
{
    ToastContent updatedContent = new ToastContent()
    {
        Duration = ToastDuration.Long,
        Visual = new ToastVisual()
        {
            BindingGeneric = new ToastBindingGeneric()
            {
                Children =
                {
                    new AdaptiveText() { Text = "可保留的测试通知" },
                    new AdaptiveText() { Text = "已为你保留在操作中心😉" }
                }
            }
        },
        Actions = new ToastActionsCustom()
        {
            Buttons =
            {
                new ToastButton("关闭通知", "action=close")
                {
                    ActivationType = ToastActivationType.Foreground
                }
            }
        }
    };

    ToastNotification updatedToast = new ToastNotification(updatedContent.GetXml());
    updatedToast.Tag = "persistent-toast";
    updatedToast.ExpirationTime = null;

    // 用新通知替换旧通知,保持在操作中心的位置
    ToastNotificationManagerCompat.CreateToastNotifier().Show(updatedToast);
}

关键注意事项

  • ExpirationTime设置:将这个属性设为null,通知就不会被系统自动过期清理;如果需要设置有效期,可以指定一个未来的DateTimeOffset值。
  • 避免自动关闭:只要不在按钮点击事件中调用ToastNotification.Dismiss()或ToastNotificationManagerCompat.History.Remove(),通知就会继续留在操作中心。
  • 使用工具库简化开发:示例中用到的Microsoft.Toolkit.Uwp.Notifications库可以大幅简化吐司通知的XML构建,比手动写XML更高效不易出错。

内容的提问来源于stack exchange,提问作者Natalia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:39:01