我想使用WPF最小化应用程序到系统托盘。 “NotifyIcon”是实现这一结果的唯一方法吗? 如果是的话,在WPF中使用“NotifyIcon”需要哪个名称空间?
如果可能的话使用“NotifyIcon”,请提供一些提示,我怎样才能在我的主窗口中使用它?
我的主窗口是,
public partial class MonthView : MetroWindow { public DateTime SelectedDate { get; set; } public MonthView() { InitializeComponent(); calMain.DisplayDate = DateTime.Today; Globals._globalController = new AppController(); Globals._globalController.appTaskManager.setupLocal(); Globals._globalController.setMonthViewWindow(this); } public void calItemSelectedDate(object sender, SelectionChangedEventArgs e) { DateTime d; if (sender is DateTime) { d = (DateTime)sender; } else { DateTime.TryParse(sender.ToString(), out d); } SelectedDate = d; ShowActivity(d); } public void ShowActivity(DateTime date) { DayView Activity = new DayView(date); Activity.Show(); this.Hide(); } private void SetButton_Click(object sender, RoutedEventArgs e) { SettingsView set = new SettingsView(); set.Show(); this.Hide(); } }
NotifyIcon没有在WPF中实现,因为它在Form中,但是仍然可以使用Windows Form NotifyIcon ,它驻留在System.Windows.Forms的 namspace中。
看看这些教程,他们可能会涵盖您的需求:
简单的解决方案,直接使用NotifyIcon: http ://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html
更高级的解决方案,基于NotifyIcon的新库有更多的功能: http : //www.codeproject.com/Articles/36468/WPF-NotifyIcon
有关NotifyIcon的更多信息可以在这里找到: http : //msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx
是的,这是可能的,我用我的个人项目成功。 Philip Sumi有一个非常好的控件http://www.hardcodet.net/projects/wpf-notifyicon 。 我用精确的一个,它的作品非常好,看起来不错(主观)。
只要注意:注意许可条款,看看你是否可以在你的项目中使用它。
您可以在App.xaml.cs中为NotifyIcon设置代码
using System.Drawing; namespace DDD { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon(); public App() { nIcon.Icon = new Icon(@"path to ico"); nIcon.Visible = true; nIcon.ShowBalloonTip(5000, "Title", "Text", System.Windows.Forms.ToolTipIcon.Info); nIcon.Click += nIcon_Click; } void nIcon_Click(object sender, EventArgs e) { //events comes here MainWindow.Visibility = Visibility.Visible; MainWindow.WindowState = WindowState.Normal; } } }
在你的Mainwindow.xaml.cs中:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.Visibility = Visibility.Hidden; }
确保Window_Closing绑定到主窗口的关闭事件。
如果你“关闭”你的主窗口,窗口的可见性将被设置为隐藏,但你的应用程序将仍然在运行。 只需点击通知区域中的NotifyIcon,就可以返回到您的窗口。