我可以在WPF中使用NotifyIcon吗?

Din*_*esh 12 c# windows wpf notifyicon wpf-controls

我想使用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();
    }

 }
Run Code Online (Sandbox Code Playgroud)

Jes*_*sen 23

NotifyIcon不是在WPF中实现的,因为它在Forms中,但您仍然可以使用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

  • 谢谢。在应用程序的引用中添加System.Windows.Forms和System.Drawing(用于Icon)后,它可以正常工作。 (2认同)

Ste*_*els 9

您可以在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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Mainwindow.xaml.cs中:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    }
Run Code Online (Sandbox Code Playgroud)

确保Window_Closing绑定到主窗口的结束事件.

如果您"关闭"主窗口,窗口可见性将设置为隐藏,但您的应用程序仍将运行.只需单击通知区域中的NotifyIcon,即可返回窗口.


Tig*_*ran 7

是的,这是可能的,我在我的个人项目中成功地使用了它.Philip Sumi撰写了一篇优秀的控件http://www.hardcodet.net/projects/wpf-notifyicon.我精确地使用了它,它的效果非常好,看起来不错(主观).

图片

只要注意:注意许可条款,看看是否可以在使用你的项目.