如何在PowerShell中处理Toast通知的点击事件?
处理PowerShell Toast通知的点击事件
要在PowerShell中处理Toast通知的激活事件(包括按钮点击、通知本身点击)以及关闭事件,无需依赖外部DLL,可通过动态创建WinRT事件委托并绑定事件的方式实现。以下是完整解决方案:
完整代码
cls Remove-Variable * -ErrorAction SilentlyContinue $ErrorActionPreference = 'Stop' # 加载必要的WinRT程序集 Add-Type -AssemblyName System.Runtime.WindowsRuntime $null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] $null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] $null = [Windows.UI.Notifications.ToastActivatedEventArgs, Windows.UI.Notifications, ContentType = WindowsRuntime] $null = [Windows.UI.Notifications.ToastDismissedEventArgs, Windows.UI.Notifications, ContentType = WindowsRuntime] $null = [Windows.Foundation.TypedEventHandler`2, Windows.Foundation, ContentType = WindowsRuntime] # 定义Toast通知内容 $toastXml = [Windows.Data.Xml.Dom.XmlDocument]::new() $xmlString = @" <toast launch="MainClick" scenario='alarm'> <visual> <binding template="ToastGeneric"> <text>确认操作</text> <text>是否执行此操作?</text> </binding> </visual> <audio src="ms-winsoundevent:Notification.Looping.Alarm" loop="true" /> <actions> <action content="是" arguments="YesAction" activationType="Foreground" /> <action content="否" arguments="NoAction" activationType="Foreground" /> </actions> </toast> "@ $toastXml.LoadXml($XmlString) # 创建Toast实例 $toast = [Windows.UI.Notifications.ToastNotification]::new($toastXml) # 定义Activated事件处理逻辑 Add-Type -TypeDefinition @" using System; using Windows.UI.Notifications; public class ToastActivationHandler { public static void OnActivated(ToastNotification sender, object args) { var activatedArgs = args as ToastActivatedEventArgs; if (activatedArgs != null) { Console.WriteLine($"操作触发: {activatedArgs.Argument}"); } } } "@ -ReferencedAssemblies System.Runtime.WindowsRuntime # 创建并绑定Activated事件委托 $activationEventHandler = [Windows.Foundation.TypedEventHandler[Windows.UI.Notifications.ToastNotification, System.Object]]::new( [ToastActivationHandler], [ToastActivationHandler].GetMethod("OnActivated") ) $toast.Add_Activated($activationEventHandler) # 定义Dismissed事件处理逻辑(处理关闭操作) Add-Type -TypeDefinition @" using System; using Windows.UI.Notifications; public class ToastDismissedHandler { public static void OnDismissed(ToastNotification sender, ToastDismissedEventArgs args) { Console.WriteLine($"Toast已关闭,原因: {args.Reason}"); } } "@ -ReferencedAssemblies System.Runtime.WindowsRuntime # 创建并绑定Dismissed事件委托 $dismissedEventHandler = [Windows.Foundation.TypedEventHandler[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications.ToastDismissedEventArgs]]::new( [ToastDismissedHandler], [ToastDismissedHandler].GetMethod("OnDismissed") ) $toast.Add_Dismissed($dismissedEventHandler) # 显示Toast通知 $appId = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe' $notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appId) $notify.Show($toast) # 保持脚本运行,等待事件触发 Write-Host "等待Toast操作...按任意键退出" $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
核心逻辑说明
加载必要类型:
除基础Toast通知类型外,额外加载ToastActivatedEventArgs、ToastDismissedEventArgs和TypedEventHandler,这些是处理事件的核心依赖。配置Toast内容:
为按钮设置activationType="Foreground",确保点击时唤醒当前PowerShell进程,保证事件能被当前脚本捕获。动态创建事件处理类:
通过Add-Type编译临时C#类,避免外部DLL依赖,类中方法负责接收WinRT事件参数并输出关键信息。绑定事件委托:
使用TypedEventHandler创建委托实例,将C#处理方法与Toast的Activated(点击操作)、Dismissed(关闭操作)事件关联。维持脚本运行:
添加ReadKey让脚本保持运行状态,避免脚本执行完成后退出,确保能捕获后续的用户操作事件。
注意事项
- 确保使用的
appId有效,示例中使用PowerShell默认注册ID,自定义ID需提前在系统中完成注册。 - 如需在事件处理中执行复杂PowerShell逻辑,可通过C#代码调用PowerShell运行空间,或通过静态变量传递数据。
内容的提问来源于stack exchange,提问作者Carsten
相关产品推荐
相关产品推荐

