如何为WPF NotifyIcon的上下文菜单按钮添加Click事件?
解决HardCodet TaskbarIcon上下文菜单按钮的Click事件绑定问题
我刚好也用过HardCodet的TaskbarIcon,这个问题其实不难解决——本质上它的ContextMenu就是标准的WPF ContextMenu,所以MenuItem的Click事件和普通WPF菜单的处理逻辑是一样的,给你两种可行的方案:
方案一:直接在XAML中绑定Click事件
这是最直观的方式,你可以直接在App.xaml的MenuItem上添加Click属性,然后在App.xaml.cs中实现对应的事件处理方法:
修改后的App.xaml代码
<Application.Resources> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:tb="http://www.hardcodet.net/taskbar"> <tb:TaskbarIcon x:Key="MyNotifyIcon" ToolTipText="Hello There"> <tb:TaskbarIcon.ContextMenu> <ContextMenu Background="White"> <MenuItem Header="Open" Click="OpenMenuItem_Click"/> <MenuItem Header="Settings" Click="SettingsMenuItem_Click"/> <MenuItem Header="Sign Out" Click="SignOutMenuItem_Click"/> <MenuItem Header="Help" Click="HelpMenuItem_Click"/> <MenuItem Header="Exit" Click="ExitMenuItem_Click"/> </ContextMenu> </tb:TaskbarIcon.ContextMenu> </tb:TaskbarIcon> </ResourceDictionary> </Application.Resources>
在App.xaml.cs中实现事件处理
using System.Windows; namespace YourAppNamespace { public partial class App : Application { private void OpenMenuItem_Click(object sender, RoutedEventArgs e) { // 获取或激活MainWindow实例 if (MainWindow == null) { MainWindow = new MainWindow(); } MainWindow.Visibility = Visibility.Visible; MainWindow.Activate(); } private void SettingsMenuItem_Click(object sender, RoutedEventArgs e) { // 这里写设置窗口的逻辑,比如打开SettingsWindow // new SettingsWindow().Show(); } private void SignOutMenuItem_Click(object sender, RoutedEventArgs e) { // 处理登出逻辑 MainWindow?.Hide(); // 其他登出操作... } private void HelpMenuItem_Click(object sender, RoutedEventArgs e) { // 打开帮助文档或窗口 } private void ExitMenuItem_Click(object sender, RoutedEventArgs e) { // 退出应用 Shutdown(); } } }
方案二:后台代码动态绑定Click事件
如果你不想在XAML中直接写事件绑定,可以在App启动后,通过资源找到TaskbarIcon,再遍历它的ContextMenu Items来绑定事件:
在App.xaml.cs的OnStartup方法中添加逻辑
using HardCodet.Wpf.TaskbarNotification; protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // 获取TaskbarIcon实例 var notifyIcon = (TaskbarIcon)FindResource("MyNotifyIcon"); if (notifyIcon?.ContextMenu != null) { foreach (var item in notifyIcon.ContextMenu.Items) { if (item is MenuItem menuItem) { switch (menuItem.Header.ToString()) { case "Open": menuItem.Click += OpenMenuItem_Click; break; case "Settings": menuItem.Click += SettingsMenuItem_Click; break; case "Sign Out": menuItem.Click += SignOutMenuItem_Click; break; case "Help": menuItem.Click += HelpMenuItem_Click; break; case "Exit": menuItem.Click += ExitMenuItem_Click; break; } } } } // 初始化MainWindow(如果未通过StartupUri指定) MainWindow = new MainWindow(); // 若需要启动时隐藏主窗口,可设置此属性 // MainWindow.Visibility = Visibility.Hidden; } // 复用方案一中的事件处理方法 private void OpenMenuItem_Click(object sender, RoutedEventArgs e) { if (MainWindow == null) { MainWindow = new MainWindow(); } MainWindow.Visibility = Visibility.Visible; MainWindow.Activate(); } // 其他事件处理方法同上...
关键注意点
- 确保后台代码中引用了
HardCodet.Wpf.TaskbarNotification命名空间,否则强制转换TaskbarIcon会失败。 - 如果你的MainWindow是通过App.xaml的
StartupUri指定的,App.Current.MainWindow会自动初始化,无需手动创建;如果是手动初始化窗口,记得赋值给MainWindow属性,方便后续调用。
内容的提问来源于stack exchange,提问作者Garvit13
相关产品推荐
相关产品推荐

