c# - 防止点击 toast 通知时再次打开应用程序

标签 c# winforms toast

我使用 Microsoft.Toolkit.Uwp.Notifications.ToastContentBuilder 在 WinForms 中为我的用户显示消息,它工作正常,但有时如果用户单击 toast,显示 toast 的程序会再次启动, 此外,如果应用程序关闭,则可以通过单击 toast 再次启动它。

我希望 Toast 只发送一条消息,并且单击它不执行任何操作。任何帮助将不胜感激。

这是我的代码

new ToastContentBuilder()
   .AddText("testing")
   .AddText("testing").SetToastDuration(ToastDuration.Long).Show();

最佳答案

对于 UWP 应用程序,您需要后台激活,但对于 WinForms 应用程序,如 documentations 中所述。 ,您需要处理前台激活,并且在不显示任何 UI 的情况下关闭应用程序。

For desktop applications, background activations are handled the same as foreground activations (your OnActivated event handler will be triggered). You can choose to not show any UI and close your app after handling activation.

因此,为了防止在应用程序关闭时单击 Toast 时显示该内容,请按照以下步骤操作:

  1. main中,调用ToastNotificationManagerCompat.WasCurrentProcessToastActivated()检查此实例是否是一个 toast 激活的实例(应用程序未运行,刚刚打开并激活以处理通知操作),然后不显示任何窗口,否则显示主窗口。

  2. 同时处理 ToastNotificationManagerCompat.OnActivated事件并检查它是否是一个 toast 激活的实例,然后什么也不做,只是退出应用程序。

示例 - 单击 Toast 通知时停止再次打开应用

在下面的 .NET 6 示例中,我显示了一些消息框来帮助您区分应用程序是 toast 激活的还是正在运行的实例。

using Microsoft.Toolkit.Uwp.Notifications;
using Windows.Foundation.Collections;
internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        //Handle when activated by click on notification
        ToastNotificationManagerCompat.OnActivated += toastArgs =>
        {
            //Get the activation args, if you need those.
            ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
            //Get user input if there's any and if you need those.
            ValueSet userInput = toastArgs.UserInput;
            //if the app instance just started after clicking on a notification 
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                MessageBox.Show("App was not running, " +
                    "but started and activated by click on a notification.");
                Application.Exit();
            }
            else
            {
                MessageBox.Show("App was running, " +
                    "and activated by click on a notification.");
            }
        };
        if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
        {
            //Do not show any window
            Application.Run();
        }
        else
        {
            //Show the main form
            Application.Run(new Form1());
        }
    }
}

了解更多:

关于c# - 防止点击 toast 通知时再次打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71213543/

相关文章:

c# - 将数据从服务器发送回客户端

android - 显示来自服务的 Toast 通知

c# - Windows 操作中心 Toast 激活

c# - 如何在 .Net 中获取视频缩略图?

c# - 在 WinForms 应用程序中拆分 wmv/wav 文件

c# - 是否可以使用 IndexOf 搜索 List<struct> 中的单个字段?

c# - 如何从 savefiledialog 获取完整路径并在 "startInfo.Arguments"中使用?

android - 当我将 startActivity(Intent) 与带有对话框主题的 Activity 一起使用时,Toast 会弹出带有 Activity 标签

c# - 正则表达式提取两个分隔符之间的字符串而不返回分隔符?

c# - 为什么启用 SSL 后仍可通过端口 389 访问事件目录?