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新手,想知道是否有人能给我一些指点,如何写一个开始最小化到tray的应用程序。其想法是,它定期获取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和我知道在XAML StartupUri指向我的主窗口,但我不知道什么合适的方法是系统托盘图标,隐藏主窗口(这也意味着当用户最小化窗口时,它应该最小化到托盘,任务栏)。
Any hints?
有提示吗?
97
There's no NotifyIcon for WPF.
WPF没有NotifyIcon。
A colleague of mine used this freely available library to good effect:
我的一个同事使用了这个免费的图书馆,效果很好:
http://www.hardcodet.net/uploads/2009/04/image13.png
http://www.hardcodet.net/uploads/2009/04/image13.png
78
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中使用NotifyIcon控件。或者也可以使用Windows API提供的Notify图标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的代码。从WPF应用程序中创建NotifyIcon组件,可以在https://github.com/wilson0x4d/mubox/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。单击和双击):
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窗口主机组件,它只是最方便的方式引入一个WPF应用程序(如一个窗口通常定义的默认入口点通过App.xaml),同样的,你不需要一个WPF包装或第三方控制,随着主权财富基金组件是保证在任何。net框架安装也有WPF支持因为它是. net框架的一部分(所有当前和未来的。net Framework版本建立)。到目前为止,微软没有迹象表明SWF支持将很快从。net框架中删除。
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是一种表示技术,通知图标是一种操作系统(而不是“表示”)概念。
47
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只是一个窗口。此时,如果您想要使用表单控件,您就必须包含框架的那个部分。我猜这取决于你到底有多纯化WPF。
If you want a quick and easy way of getting started check out this WPF NotifyIcon control on the Code Project which does not rely on the WinForms NotifyIcon at all. A more recent version seems to be available on the author's website and as a NuGet package. This seems like the best and cleanest way to me so far.
如果您想要一种快速简便的方法,可以在代码项目中查看WPF NotifyIcon控件,它不依赖于winform NotifyIcon。一个更近期的版本似乎可以在作者的网站和NuGet包中找到。这似乎是迄今为止对我最好和最干净的方式。
- Rich ToolTips rather than text
- 丰富的工具提示而不是文本。
- WPF context menus and popups
- WPF上下文菜单和弹出窗口
- Command support and routed events
- 命令支持和路由事件
- Flexible data binding
- 灵活的数据绑定
- Rich balloon messages rather than the default messages provides by the OS
- 富气球消息而不是操作系统提供的默认消息
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提要,我将它用于类似的目的。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/09/24/17d1b78b6560e24089a7325a34333274.html。