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

时间:2013-07-31 14:05:32

标签: c# wpf icons hide

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

 }

2 个答案:

答案 0 :(得分:0)

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

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

C# WPF - Application Icon + ShowInTaskbar = False

答案 1 :(得分:0)

为什么不让Windows共享一个NotifyIcon?切换视图时,可以切换NotifyIcon的图标。你已经拥有一个全球性的“单身人士”。

为每个屏幕创建一个新图标意味着那里有四个图标,并且由于您遍历静态参考,您将无法再访问NotifyIcon。