首先,我们在PageBase中添加一个Main属性。
    1     public Page Main
    2     {
    3         get { return ( Page) Application.Current.Properties[ "Main"]; }
    4     }
  然后在Login的界面中,将Main窗体的实例存储到系统属性中。注意,WPF中没有Session可用,不过我们可以将值存储在Application.Current.Properties中。
  这样一来,在任何一个继承于PageBase的窗口中,都可以通过this.Main的方式来访问主窗体的实例。
    1     Application.Current.Properties[ "Main"] = new Main();
    2 
    3     this.NavigationService.Navigate( this.Main);
  之后,我们可以在Main窗口的类中添加如下的代码,用于显示某个新窗口,任何继承于PageBase的窗口都可以通过访问this.Main.ShowListPage的方式来创建并显示一个新窗体。注意,由于具体显示什么窗体需要通过参数传进来,所以我们采用Activator.CreateInstance的方式来创建新窗口。
  代码最后的Navigate方法用于显示新窗口。
    1     public void ShowListPage<T>()
    2     {
    3         try
    4         {
    5             PageBase page = null;
    6             Type type = typeof(T);
    7 
    8             if (type.Equals( typeof(Department. List))) page = this._DepartmentList;
    9             if (type.Equals( typeof(Employee. List))) page = this._EmployeeList;
   10 
   11             if (page == null)
   12             {
   13                 page = ( PageBase) Activator.CreateInstance(type);
   14 
   15                 page.Closed += new EventHandler(Page_Closed);
   16             }
   17 
   18             if (type.Equals( typeof(Department. List))) this._DepartmentList = (Department. List)page;
   19             if (type.Equals( typeof(Employee. List))) this._EmployeeList = (Employee. List)page;
   20 
   21             this.Navigate(page);
   22         }
   23         catch
   24         {
   25             throw;
   26         }
   27     }
  在XBAP程序中,显示一个新窗口并不是指弹出一个新的窗口,而是将新窗口显示在Main窗口的一个Frame控件中。该控件的创建方法如下:
    1      < Frame Name ="frmFrame" DockPanel.Dock ="Bottom" Margin ="0,26,0,0" />
  然后,我们即可以很简单的用Frame控件的Navigate方法实现显示新窗口了。在显示之前,我们先将其它窗口都设为隐藏状态,以便保留该窗口的当前状态。注意,不是关闭,只有用于点击窗口中的Close按钮时,才不需要保留窗口的当前状态。
    1     private void Navigate( Page page)
    2     {
    3         try
    4         {
    5             if ( this._DepartmentList != null) this._DepartmentList.Visibility = Visibility.Hidden;
    6             if ( this._EmployeeList != null) this._EmployeeList.Visibility = Visibility.Hidden;
    7 
    8             page.Visibility = Visibility.Visible;
    9 
   10             this.WindowTitle = this.SystemTitle + this.SystemSeparator + this.Title + this.SystemSeparator + page.Title;
   11 
   12             this.frmFrame.Navigate(page);
   13         }
   14         catch
   15         {
   16             throw;
   17         }
   18     }
  由于我们创建新窗体时,为新窗体添加了一个关闭事件(关闭事件PageBase中定义),那么这里就可以处理关闭事件了。关闭时并不是真正关闭一个窗体,而是将存储该窗体实例的变量设为null即可。
    1     private void Page_Closed( object sender, EventArgs e)
    2     {
    3         try
    4         {
    5             (sender as PageBase).Visibility = Visibility.Hidden;
    6 
    7             if (sender is Department. List) this._DepartmentList = null;
    8             if (sender is Employee. List) this._EmployeeList = null;
    9 
   10             this.WindowTitle = this.SystemTitle + this.SystemSeparator + this.Title;
   11         }
   12         catch
   13         {
   14             throw;
   15         }
   16     }