右键单击系统托盘中的上下文菜单
我有一个在系统托盘中运行的 WPF 应用程序。我正在尝试创建一个上下文菜单,当您右键单击托盘中的图标时会弹出该菜单。这是 XAML:
<Window.Resources>
<ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
<MenuItem Header="Exit" Click="Menu_Exit"/>
</ContextMenu>
</Window.Resources>
这是隐藏代码:
void NotifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
var menu = this.FindResource("NotifierContextMenu") as ContextMenu;
menu.IsOpen = true;
}
}
protected void Menu_Exit(object sender, RoutedEventArgs e)
{
NotifyIcon.Visible = false;
Application.Current.Shutdown();
}
我遇到的问题是,当您右键单击该图标时,它会抛出一个错误,指出 NotifierContextMenu 无法找到。我缺少什么?
I have a WPF app that runs in the system tray. I'm trying to create a context menu that pops up when you right click on the icon in the tray. Here is the XAML:
<Window.Resources>
<ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
<MenuItem Header="Exit" Click="Menu_Exit"/>
</ContextMenu>
</Window.Resources>
And here is the code-behind:
void NotifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
var menu = this.FindResource("NotifierContextMenu") as ContextMenu;
menu.IsOpen = true;
}
}
protected void Menu_Exit(object sender, RoutedEventArgs e)
{
NotifyIcon.Visible = false;
Application.Current.Shutdown();
}
The issue that I'm having is that when you right-click on the icon, it throws an error that NotifierContextMenu can't be found. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己尝试过,没有任何问题。您的 MouseDown 事件处理程序实际上是创建 NotifierContextMenu 的同一类的一部分,对吗?
也许尝试编写一些代码来列出资源,看看是否可以匹配它所引用的资源集。
I tried this myself with no problems. Your event handler for the MouseDown is in fact part of the same class that NotifierContextMenu is created right?
Perhaps try making a little code to list out the resources to see if you can match up which resource set it is referring to.