Frage

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?

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

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

 }
War es hilfreich?

Lösung

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

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

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

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

Andere Tipps

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

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:

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.

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.

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).

image

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top