当导航到另一个窗口时,隐藏主窗口的图标

本文关键字:窗口 隐藏 图标 导航 另一个 | 更新日期: 2023-09-27 18:25:35

在我的Wpf应用程序中,我有四个窗口。对于每个窗口,我都写了以下代码,这样它就可以最小化到系统托盘。但问题是,当从一个窗口导航到另一个窗口时,第一个窗口的图标仍然出现在系统托盘中。我只是想在导航到另一个窗口时隐藏那个图标?请建议如何进行?

我的主窗口代码是-

public partial class MonthView : MetroWindow
{
    public DateTime SelectedDate { get; set; }
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem menuItem2;
    public MonthView()
    {
            InitializeComponent();
            calMain.DisplayDate = DateTime.Today;
            Globals._globalController = new AppController();
            Globals._globalController.appTaskManager.setupLocal();
            Globals._globalController.setMonthViewWindow(this);
            Globals.ni = new NotifyIcon();
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            Globals.ni.Icon = TimeSheet.Properties.Resources.MonthViewIcon;
            Globals.ni.Visible = true;
            Globals.ni.Click +=
            delegate(object sender, EventArgs args)
            {
                this.Show();
                this.WindowState = WindowState.Normal;
            };
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1 });
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2 });
            this.menuItem1.Text = "Start";
            this.menuItem2.Text = "Exit";
            Globals.ni.ContextMenu = this.contextMenu1;
    }
    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
        {
            this.Hide();
            Globals.ni.BalloonTipTitle = "MonthView";
            Globals.ni.BalloonTipText = "This is main window";
            Globals.ni.Visible = true;
            Globals.ni.ShowBalloonTip(500);
            base.OnStateChanged(e);
        }
    }
    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?切换视图时,可以切换NotifyIcon的图标。你已经有了一个全局的"singleton"

为每个屏幕创建一个新图标意味着那里有四个图标,并且由于您走过静态引用,您不再可以访问NotifyIcon。

WPF Window具有ShowInTaskbar属性。将其设置为false

你可能也对这篇文章感兴趣:

C#WPF-应用程序图标+ShowInTaskbar=错误