2013-07-31 62 views
0

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

} 
+0

如果這是一個WPF項目,爲什麼使用'System.Windows.Forms.ContextMenu's和'System.Windows.Forms.MenuItem's?否則,如果這不是WPF項目,爲什麼要添加WPF標記? – Sheridan

回答

0

爲什麼沒有Windows共享一個NotifyIcon的?切換視圖時,您可以切換NotifyIcon的圖標。你已經擁有了一個全球性的「單身人士」。

爲每個屏幕創建一個新圖標意味着這裏有四個圖標,而且由於您瀏覽了靜態引用,因此您無法再訪問NotifyIcon。

+0

但是這提出了一個問題。如果你只允許一個視圖,爲什麼不直接在一個位於主窗口的控件上創建視圖。 –

+0

我認爲,這是很好的解決方案。值得去嘗試。 – user2622971

+0

這工作正常,謝謝。 – user2622971