托盘图标的 WPF 上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16546296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:47:44  来源:igfitidea点击:

WPF ContextMenu for tray icon

c#wpfcontextmenutray

提问by Joetjah

I'm having a WPF application which I can minimize to tray. When I normal-click it, the window shows again.

我有一个 WPF 应用程序,我可以将其最小化到托盘。当我正常单击它时,窗口再次显示。

Now I'm wondering how to create a simple ContextMenu?

现在我想知道如何创建一个简单的ContextMenu?

The ContextMenuhas to get filled with x options which onclick will run a function. For now I just need an 'Exit'-item linked to an 'Exit_Click' method.

ContextMenu要得到充满了的onclick将运行一个函数x选项。现在我只需要一个链接到“Exit_Click”方法的“退出”项。

Something I've tried is:

我试过的东西是:

ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
menu.IsOpen = true;

menudoesn't know of any IsOpenvalue.

menu不知道有什么IsOpen价值。

Other examples like to use a lot of different things. One of them requires me to create a HostManager for some reason.

其他例子喜欢使用很多不同的东西。其中之一要求我出于某种原因创建一个 HostManager。

I just need a simple ContextMenu. How can I achieve this?

我只需要一个简单的ContextMenu. 我怎样才能做到这一点?

回答by Viv

As @H.B. mentioned Hardcodet's NotifyIconis pretty darn good for WPF Taskbar icons. Sucks you don't get that out of the box with WPF but you might as well use it and address your issue than wait for Microsoft to fix it(They really should just add that library into standards)

正如@HB 提到的,Hardcodet的 NotifyIcon非常适合 WPF 任务栏图标。糟透了,你没有用 WPF 开箱即用,但你不妨使用它并解决你的问题,而不是等待微软修复它(他们真的应该将该库添加到标准中)

Now to solve your issue(Using above solution):

现在解决您的问题(使用上述解决方案):

  • Download the solution
  • Build the library
  • Add it to your source control if you have one and add a reference to it(Hardcodet.Wpf.TaskbarNotification.dll) in your project
  • 下载解决方案
  • 建立图书馆
  • 如果你有的话,把它添加到你的源代码管理中,并Hardcodet.Wpf.TaskbarNotification.dll在你的项目中添加对它的引用()

Now in your MainWindow.xaml you could just have something like:

现在在您的 MainWindow.xaml 中,您可以拥有如下内容:

<Window ...
        xmlns:tb="http://www.hardcodet.net/taskbar"
        ...>
  ...
  <Grid>
    <tb:TaskbarIcon>
      <tb:TaskbarIcon.ContextMenu>
        <ContextMenu>
          <MenuItem Click="Exit_Click"
                    Header="Exit" />
        </ContextMenu>
      </tb:TaskbarIcon.ContextMenu>
    </tb:TaskbarIcon>
    ...
  </Grid>
</Window>

and MainWindow.xaml.cs with the click handler like you needed:

和 MainWindow.xaml.cs 以及您需要的点击处理程序:

private void Exit_Click(object sender, RoutedEventArgs e) {
  Application.Current.Shutdown();
}

I do recommend spending some time looking at the examples coming with the source code of the library to familiarize yourself with your available options. Trust me wpf has it way too easy when it comes to helper libraries. Try some of qt helper libraries and you'll know what "buried in there somewhere" literally means in opensource helpers.

我确实建议花一些时间查看库源代码附带的示例,以熟悉可用选项。相信我 wpf 在帮助程序库方面太容易了。尝试一些 qt 助手库,你就会知道“埋在某处”在开源助手中的字面意思。