簡體   English   中英

我可以在 WPF 中使用 NotifyIcon 嗎?

[英]Can I use NotifyIcon in WPF?

我想使用 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不像在 Forms 中那樣在 WPF 中實現,但您仍然可以使用 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

您可以在 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,就會返回您的窗口。

是的,這是可能的,我在我的個人項目中成功地使用了它。 有一個由 Philip Sumi http://www.hardcodet.net/projects/wpf-notifyicon編寫的優秀控件。 我恰好使用了那個,它的效果非常好,看起來不錯(主觀)。

圖片

請注意:注意許可條款,檢查是否可以在您的項目中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM