C# 只有托盘图标的 WPF 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1472633/
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
WPF Application that only has a tray icon
提问by Michael Stum
I am a total WPF newbie and wonder if anyone could give me some pointers how to write an application that starts minimized to tray. The idea is that it periodically fetches an RSS Feed and creates a Toaster-Popup when there are new feeds.
我是一个完全的 WPF 新手,想知道是否有人能给我一些指导,如何编写一个开始最小化到托盘的应用程序。这个想法是它会定期获取一个 RSS 提要,并在有新提要时创建一个 Toaster-Popup。
The Application should still have a Main Window (essentially just a list containing all feed entries), but that should be hidden by default.
应用程序仍然应该有一个主窗口(本质上只是一个包含所有提要条目的列表),但默认情况下应该是隐藏的。
I have started reading about XAML and WPF and I know that the StartupUri in the App.xaml has to point to my main window, but I have no idea what the proper way is to do the SysTray icon and hide the main window (this also means that when the user minimizes the window, it should minimize to tray, not to taskbar).
我已经开始阅读有关 XAML 和 WPF 的内容,我知道 App.xaml 中的 StartupUri 必须指向我的主窗口,但我不知道执行 SysTray 图标并隐藏主窗口的正确方法是什么(这也是意味着当用户最小化窗口时,它应该最小化到托盘,而不是任务栏)。
Any hints?
任何提示?
采纳答案by Drew Noakes
There's no NotifyIcon for WPF.
WPF 没有 NotifyIcon。
A colleague of mine used this freely available library to good effect:
我的一位同事使用了这个免费提供的库,效果很好:
- http://www.hardcodet.net/wpf-notifyicon(blog post)
- https://bitbucket.org/hardcodet/notifyicon-wpf/src(source code)
- https://www.nuget.org/packages/Hardcodet.NotifyIcon.Wpf/(NuGet package)
- http://visualstudiogallery.msdn.microsoft.com/aacbc77c-4ef6-456f-80b7-1f157c2909f7/
回答by Dale
I recently had this same problem. Unfortunately, NotifyIcon is only a Windows.Forms control at the moment, if you want to use it you are going to have to include that part of the framework. I guess that depends how much of a WPF purist you are.
我最近遇到了同样的问题。不幸的是,NotifyIcon 目前只是一个 Windows.Forms 控件,如果你想使用它,你将不得不包含框架的那部分。我想这取决于您是多少 WPF 纯粹主义者。
If you want a quick and easy way of getting started check out this WPF NotifyIcon controlon the Code Projectwhich does not rely on the WinForms NotifyIcon at all. A more recent version seems to be available on the author's websiteand as a NuGet package. This seems like the best and cleanest way to me so far.
如果您想要一种快速简便的入门方法,请查看Code Project 上的这个WPF NotifyIcon 控件,它根本不依赖 WinForms NotifyIcon。作者的网站上似乎提供了一个更新的版本,并作为NuGet 包提供。到目前为止,这对我来说似乎是最好和最干净的方式。
- Rich ToolTips rather than text
- WPF context menus and popups
- Command support and routed events
- Flexible data binding
- Rich balloon messages rather than the default messages provides by the OS
- 丰富的工具提示而不是文本
- WPF 上下文菜单和弹出窗口
- 命令支持和路由事件
- 灵活的数据绑定
- 丰富的气球消息而不是操作系统提供的默认消息
Check it out. It comes with an amazing sample app too, very easy to use, and you can have great looking Windows Live Messenger style WPF popups, tooltips, and context menus. Perfect for displaying an RSS feed, I am using it for a similar purpose.
一探究竟。它还附带了一个很棒的示例应用程序,非常易于使用,并且您可以拥有漂亮的 Windows Live Messenger 风格的 WPF 弹出窗口、工具提示和上下文菜单。非常适合显示 RSS 提要,我将它用于类似目的。
回答by Shaun Wilson
You have to use the NotifyIcon control from System.Windows.Forms, or alternatively you can use the Notify Icon API provided by Windows API. WPF Provides no such equivalent, and it has been requested on Microsoft Connect several times.
您必须使用 System.Windows.Forms 中的 NotifyIcon 控件,或者您也可以使用 Windows API 提供的 Notify Icon API。WPF 不提供此类等效项,并且已在 Microsoft Connect 上多次请求。
I have code on GitHub which uses System.Windows.Forms
NotifyIcon Component from within a WPF application, the code can be viewed at https://github.com/wilson0x4d/Mubox/blob/master/Mubox.QuickLaunch/AppWindow.xaml.cs
我在 GitHub 上有代码,它使用System.Windows.Forms
WPF 应用程序中的 NotifyIcon 组件,可以在https://github.com/wilson0x4d/Mubox/blob/master/Mubox.QuickLaunch/AppWindow.xaml.cs查看代码
Here are the summary bits:
以下是摘要位:
Create a WPF Window with ShowInTaskbar=False, and which is loaded in a non-Visible State.
使用 ShowInTaskbar=False 创建一个 WPF 窗口,该窗口以非可见状态加载。
At class-level:
在班级:
private System.Windows.Forms.NotifyIcon notifyIcon = null;
During OnInitialize():
在 OnInitialize() 期间:
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Click += new EventHandler(notifyIcon_Click);
notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
notifyIcon.Icon = IconHandles["QuickLaunch"];
During OnLoaded():
在 OnLoaded() 期间:
notifyIcon.Visible = true;
And for interaction (shown as notifyIcon.Click and DoubleClick above):
对于交互(如上面的 notifyIcon.Click 和 DoubleClick 所示):
void notifyIcon_Click(object sender, EventArgs e)
{
ShowQuickLaunchMenu();
}
From here you can resume the use of WPF Controls and APIs such as context menus, pop-up windows, etc.
从这里您可以继续使用 WPF 控件和 API,例如上下文菜单、弹出窗口等。
It's that simple. You don't exactly need a WPF Window to host to the component, it's just the most convenient way to introduce one into a WPF App (as a Window is generally the default entry point defined via App.xaml), likewise, you don't need a WPF Wrapper or 3rd party control, as the SWF component is guaranteed present in any .NET Framework installation which also has WPF support since it's part of the .NET Framework (which all current and future .NET Framework versions build upon.) To date, there is no indication from Microsoft that SWF support will be dropped from the .NET Framework anytime soon.
就这么简单。您并不完全需要 WPF Window 来托管组件,这只是将一个 WPF 应用程序引入 WPF 应用程序的最便捷方式(因为 Window 通常是通过 App.xaml 定义的默认入口点),同样,您不需要不需要 WPF Wrapper 或第 3 方控件,因为 SWF 组件保证存在于任何也支持 WPF 的 .NET Framework 安装中,因为它是 .NET Framework 的一部分(所有当前和未来的 .NET Framework 版本都基于它构建。)迄今为止,Microsoft 没有任何迹象表明 .NET Framework 将很快取消对 SWF 的支持。
Hope that helps.
希望有帮助。
It's a little cheese that you have to use a pre-3.0 Framework Component to get a tray-icon, but understandably as Microsoft has explained it, there is no concept of a System Tray within the scope of WPF. WPF is a presentation technology, and Notification Icons are an Operating System (not a "Presentation") concept.
您必须使用 3.0 之前的框架组件来获取托盘图标有点困难,但可以理解的是,正如微软所解释的那样,WPF 范围内没有系统托盘的概念。WPF 是一种演示技术,而通知图标是一种操作系统(不是“演示”)概念。