C# 我可以在 WPF 中使用 NotifyIcon 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17674761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:09:58  来源:igfitidea点击:

Can I use NotifyIcon in WPF?

c#wpfwindowswpf-controlsnotifyicon

提问by Dinesh

I want to minimizing application to system tray using WPF. Is "NotifyIcon" is the only way to achieve this result? If yes, which namespace is required for using "NotifyIcon" in WPF?

我想使用 WPF 将应用程序最小化到系统托盘。“NotifyIcon”是实现此结果的唯一方法吗?如果是,在 WPF 中使用“NotifyIcon”需要哪个命名空间?

If possible with "NotifyIcon",please provide some hint, how can I use that in my Mainwindow?

如果可以使用“NotifyIcon”,请提供一些提示,我如何在主窗口中使用它?

My main window is,

我的主窗口是,

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();
    }

 }

采纳答案by Jesper Jensen

NotifyIconis not implemented in WPF as it is in Forms, but you can still use the Windows Form NotifyIcon, it resides in the System.Windows.Formsnamspace.

NotifyIcon不像在 Forms 中那样在 WPF 中实现,但您仍然可以使用 Windows Form NotifyIcon,它驻留在System.Windows.Formsnamspace 中。

Take a look at these tutorials, they might cover your needs:

看看这些教程,它们可能满足您的需求:

Simple solution, directly using NotifyIcon: http://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html

简单的解决方案,直接使用NotifyIcon:http://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html

More advanced solution, new library based on NotifyIconwith more features: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon

更高级的解决方案,基于NotifyIcon 的新库,具有更多功能:http: //www.codeproject.com/Articles/36468/WPF-NotifyIcon

More info about NotifyIconcan be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

可以在此处找到有关NotifyIcon 的更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

回答by Tigran

Yes, it is possible, and I used it with success in my personal project. There is an excelent control written by Philip Sumi http://www.hardcodet.net/projects/wpf-notifyicon. I used precisly that one and it works really great and looks nice (subjective).

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

image

图片

Just note: pay attention licensing terms, check out if you can use it in yourproject.

请注意:注意许可条款,检查是否可以在您的项目中使用它。

回答by Stef Geysels

You can set your code for the NotifyIcon in App.xaml.cs

您可以在 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;
        }
    }
}

And in your Mainwindow.xaml.cs:

在您的 Mainwindow.xaml.cs 中:

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

Make sure that Window_Closing is binded to the closing event of the main window.

确保 Window_Closing 绑定到主窗口的关闭事件。

If you "close" your main window, the window visibility wil be set to hidden but your app will be still running. Simple click on the NotifyIcon in the notification area and there is your window back.

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