如何防止在单击 Windows Toast 时启动 UWP 应用程序

Yog*_*esh 0 .net c# windows-10 uwp

我有一个显示祝酒词的 UWP 应用程序。在 toast 通知中心单击该 toast 时,如果应用程序未启动,它始终会启动该应用程序。我不想要那个。我的吐司纯粹是为了向用户显示消息的信息目的。单击 toast 时,应将其关闭。它不应该启动应用程序。

早些时候,我的印象是,如果我们在 toast 中给 Launch 参数,那么只有它应该启动应用程序,但看起来 Launch 无关紧要。它只需单击 toast body 即可启动应用程序。

我想阻止在单击 Toast 时启动应用程序。

我试过以下选项

   protected override void OnActivated(IActivatedEventArgs args)
    {
        if( args is ToastNotificationActivatedEventArgs)
        {
            if(args.Kind == ActivationKind.ToastNotification)
            {
                Application.Current.Exit();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但这将启动应用程序,然后关闭应用程序。它将显示启动画面,用户将看到应用程序突然关闭。此外,如果我不想要的应用程序已经启动,它将关闭原始工作应用程序。

小智 5

UWP 可以在发送通知时设置通知活动类型:

var content = new ToastContent
{
    Launch = "...",
    ActivationType = ToastActivationType.Background,
    Visual = new ToastVisual()
    {
        ...
    }
};
var notifier = ToastNotificationManager.CreateToastNotifier();
var notification = new ToastNotification(content.GetXml());
notifier.Show(notification);
Run Code Online (Sandbox Code Playgroud)

ActivationType设置为ToastActivationType.Background,应用程序将调用已注册的后台任务来处理相应的内容,而不是启动应用前景的。

有关后台通知的详细信息,您可以参考以下文档:

要使用type来表示ToastContent,需要安装Microsoft.Toolkit.Uwp.Notificationsnuget包