【发布时间】:2011-01-15 16:32:53
【问题描述】:
我有一个在系统托盘中运行的 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 的错误。我错过了什么?
【问题讨论】:
标签: wpf contextmenu