简体   繁体   English

只有托盘图标的 WPF 应用程序

[英]WPF Application that only has a tray icon

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.我是一个完全的 WPF 新手,想知道是否有人能给我一些指导,如何编写一个开始最小化到托盘的应用程序。 The idea is that it periodically fetches an RSS Feed and creates a Toaster-Popup when there are new feeds.这个想法是它会定期获取一个 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?任何提示?

You have to use the NotifyIcon control from System.Windows.Forms, or alternatively you can use the Notify Icon API provided by Windows API.您必须使用 System.Windows.Forms 中的 NotifyIcon 控件,或者您也可以使用 Windows API 提供的 Notify Icon API。 WPF Provides no such equivalent, and it has been requested on Microsoft Connect several times. 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 上有代码,它使用 WPF 应用程序中的System.Windows.Forms 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 应用程序的最便捷方式(因为 Window 通常是通过 App.xaml 定义的默认入口点),同样,您不需要不需要 WPF 包装器或第 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.您必须使用 3.0 之前的框架组件来获取托盘图标有点困难,但可以理解的是,正如微软所解释的那样,WPF 范围内没有系统托盘的概念。 WPF is a presentation technology, and Notification Icons are an Operating System (not a "Presentation") concept. WPF 是一种演示技术,而通知图标是一种操作系统(不是“演示”)概念。

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.不幸的是,NotifyIcon 目前只是一个 Windows.Forms 控件,如果你想使用它,你将不得不包含框架的那部分。 I guess that depends how much of a WPF purist you are.我想这取决于您是多少 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.如果您想要一种快速简便的入门方法,请查看Code Project 上的这个WPF NotifyIcon 控件它根本不依赖 WinForms NotifyIcon。 A more recent version seems to be available on the author's website and as a NuGet package .作者的网站上似乎提供一个更新的版本,并作为NuGet 包提供 This seems like the best and cleanest way to me so far.到目前为止,这对我来说似乎是最好和最干净的方式。

  • 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.它还附带了一个很棒的示例应用程序,非常易于使用,并且您可以拥有漂亮的 Windows Live Messenger 风格的 WPF 弹出窗口、工具提示和上下文菜单。 Perfect for displaying an RSS feed, I am using it for a similar purpose.非常适合显示 RSS 提要,我将它用于类似目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM