我有一个在系统托盘中运行的WPF应用程序.我正在尝试创建一个上下文菜单,当您右键单击托盘中的图标时,该菜单会弹出.这是XAML:
<Window.Resources>
<ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
<MenuItem Header="Exit" Click="Menu_Exit"/>
</ContextMenu>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
这是代码隐藏:
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();
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当您右键单击该图标时,它会抛出NotifierContextMenu无法找到的错误.我错过了什么?
我自己尝试过,没有任何问题。您的 MouseDown 事件处理程序实际上是创建 NotifierContextMenu 的同一类的一部分,对吗?
也许尝试编写一些代码来列出资源,看看是否可以匹配它所引用的资源集。
| 归档时间: |
|
| 查看次数: |
4904 次 |
| 最近记录: |