【问题标题】:Hide the icon of main window when navigate to another window导航到另一个窗口时隐藏主窗口的图标
【发布时间】:2013-07-31 14:05:31
【问题描述】:

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

 }

【问题讨论】:

  • 如果这是一个 WPF 项目,为什么要使用 System.Windows.Forms.ContextMenus 和 System.Windows.Forms.MenuItems?否则,如果这不是 WPF 项目,为什么还要添加 WPF 标签?

标签: c# wpf icons hide


【解决方案1】:

为什么不让 windows 共享一个 NotifyIcon?您可以在切换视图时切换 NotifyIcon 的图标。您已经拥有一个全局“单例”。

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

【讨论】:

  • 但这带来了一个问题。如果您只允许一个视图,为什么不简单地在位于主窗口的控件中创建视图。
【解决方案2】:

WPF Windows 有一个 ShowInTaskbar 属性。将此设置为false

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

C# WPF - Application Icon + ShowInTaskbar = False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多