4. 打开ContactDetailPage1.xaml.cs,在代码文件中,同样的,将类型参数值改成Object。
我们想以两种模式显示这个页面:Read和Edit。我们可以重用Frame_RightPane,也就是显示联系人详细信息的面板上面的Read模式。我们为ContactDetailPage1定义一个构造函数,接受一个Boolean值来设置是否激活Edit模式,还接受一个int类型的索引,引用到ContactList里面的一个联系人项,也就是当前显示在页面上的这个联系人。
/// <summary>
/// Creates a Contact Detail page, specifying
/// edit mode and index of ContactList item to
/// be displayed when in Read mode.
/// </summary>
public ContactDetailPage1(bool editmode, int itemNumber)
{
InitializeComponent();
this.KeepAlive = true;
// if in read mode...
if (editmode == false)
{
ContactList cl =
Application.Current.Properties["ContactList"]
as ContactList;
// if the contact list is populated get an object using index
if (cl != null && cl.Count > 0)
{
Contact c = cl[itemNumber];
this.RootGrid.DataContext = c;
// set the input fields to read only
this.txtEmailAddress.IsEnabled = false;
this.txtFirstName.IsEnabled = false;
this.txtLastName.IsEnabled = false;
this.ButtonPanel1.Visibility = Visibility.Hidden;
this.ButtonPanel2.Visibility = Visibility.Hidden;
}
}
else
{
this.btnHomePage.Visibility = Visibility.Hidden;
this.btnEmailAddress.Visibility = Visibility.Hidden;
}
}
/// Creates a Contact Detail page, specifying
/// edit mode and index of ContactList item to
/// be displayed when in Read mode.
/// </summary>
public ContactDetailPage1(bool editmode, int itemNumber)
{
InitializeComponent();
this.KeepAlive = true;
// if in read mode...
if (editmode == false)
{
ContactList cl =
Application.Current.Properties["ContactList"]
as ContactList;
// if the contact list is populated get an object using index
if (cl != null && cl.Count > 0)
{
Contact c = cl[itemNumber];
this.RootGrid.DataContext = c;
// set the input fields to read only
this.txtEmailAddress.IsEnabled = false;
this.txtFirstName.IsEnabled = false;
this.txtLastName.IsEnabled = false;
this.ButtonPanel1.Visibility = Visibility.Hidden;
this.ButtonPanel2.Visibility = Visibility.Hidden;
}
}
else
{
this.btnHomePage.Visibility = Visibility.Hidden;
this.btnEmailAddress.Visibility = Visibility.Hidden;
}
}
5. 添加一个Cancel和一个Finish的事件处理函数。OnCancelClick方法用来关闭窗口。OnFinishClick从额面上读出输入框里的内容,组成一个联系人对象,并将它传递给Page Function注册的返回句柄。
private void OnCancelClick(object sender, RoutedEventArgs e)
{
((NavigationWindow)(this.Parent)).Close();
}
private void OnFinishClick(object sender, RoutedEventArgs e)
{
ContactList cl = Application.Current.Properties["ContactList"]
as ContactList;
Contact c = new Contact();
c.FirstName = this.txtFirstName.Text;
c.LastName = this.txtLastName.Text;
c.HomePage = new Uri(this.txtHomePage.Text);
c.EmailAddress = this.txtEmailAddress.Text;
OnReturn(new ReturnEventArgs<object>(c));
}
{
((NavigationWindow)(this.Parent)).Close();
}
private void OnFinishClick(object sender, RoutedEventArgs e)
{
ContactList cl = Application.Current.Properties["ContactList"]
as ContactList;
Contact c = new Contact();
c.FirstName = this.txtFirstName.Text;
c.LastName = this.txtLastName.Text;
c.HomePage = new Uri(this.txtHomePage.Text);
c.EmailAddress = this.txtEmailAddress.Text;
OnReturn(new ReturnEventArgs<object>(c));
}
6.下面我们为ContactDetailPage1这个功能页面创建返回事件处理函数。这就是调用页面。打开UILessPageFunction.xaml.cs这个代码文件,添加返回事件的处理函数。我们会在UILessPageFunction的OnLoaded方法里初始化一个ContactDetailPage1页面。同样的,将类型参数从默认值String改成Object。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AddressBook
{
/// <summary>
/// Interaction logic for UILessPageFunction.xaml
/// </summary>
public partial class UILessPageFunction : PageFunction<Object>
{
public UILessPageFunction()
{
}
//
// The OnLoaded handler is run automatically when the class is loaded
//
private void OnLoaded(object sender, RoutedEventArgs e)
{
// navigate to the first of two page functions which
// have 'add contact' UI
ContactDetailPage1 pageFunction =
new ContactDetailPage1(true, 0);
pageFunction.Return +=
new ReturnEventHandler<object>(pageFunction1_Return);
NavigationService.GetNavigationService(this).
Navigate(pageFunction);
}
//
// This is the ContactDetailPage1 page function's return handler
//
void pageFunction1_Return(object sender, ReturnEventArgs<object> e)
{
// get a reference to the address book's contact list
ContactList cl =
Application.Current.Properties["ContactList"]
as ContactList;
Contact c = e.Result as Contact;
// add the newly created contact to the list
cl.Add(c);
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AddressBook
{
/// <summary>
/// Interaction logic for UILessPageFunction.xaml
/// </summary>
public partial class UILessPageFunction : PageFunction<Object>
{
public UILessPageFunction()
{
}
//
// The OnLoaded handler is run automatically when the class is loaded
//
private void OnLoaded(object sender, RoutedEventArgs e)
{
// navigate to the first of two page functions which
// have 'add contact' UI
ContactDetailPage1 pageFunction =
new ContactDetailPage1(true, 0);
pageFunction.Return +=
new ReturnEventHandler<object>(pageFunction1_Return);
NavigationService.GetNavigationService(this).
Navigate(pageFunction);
}
//
// This is the ContactDetailPage1 page function's return handler
//
void pageFunction1_Return(object sender, ReturnEventArgs<object> e)
{
// get a reference to the address book's contact list
ContactList cl =
Application.Current.Properties["ContactList"]
as ContactList;
Contact c = e.Result as Contact;
// add the newly created contact to the list
cl.Add(c);
}
}
}