技术开发 频道

使用WPF新功能构建丰富的客户端应用

  9.ContactDetailPage2功能页的代码非常直观。再次强调要把类型参数改为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 ContactDetailPage2.xaml
    
/// </summary>
    public partial class ContactDetailPage2 : PageFunction<Object>
    {
        
public ContactDetailPage2()
        {
            InitializeComponent();
        }


        
/// <summary>
        
/// Create a ContactDetailPage2 with specified edit mode value.
        
/// Since we won’t be displaying this page on the right hand side
        
/// Frame, this constructor does not accept and use the index
        
/// of selected element in the ContactList.
        
/// </summary>
        public ContactDetailPage2(bool editmode)
        {
            
            InitializeComponent();

            
if (editmode == false)
            {
                ContactList cl
=
                           Application.Current.Properties[
"ContactList"]
                                      
as ContactList;
                Contact c
= cl[0];
                
this.RootGrid.DataContext = c;

                
this.RootGrid.IsEnabled = false;
                
this.ButtonPanel1.Visibility = Visibility.Hidden;
                
this.ButtonPanel2.Visibility = Visibility.Hidden;
            }
        }

        
//
        
// Navigate back if the Back button is clicked
        
//
        private void OnBackClick(object sender, RoutedEventArgs e)
        {
            NavigationService ns
=
                         NavigationService.GetNavigationService(
this);
            ns.GoBack();

        }


        
//
        
// Close window if Cancel button is clicked
        
//
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            ((NavigationWindow)(
this.Parent)).Close();

        }

        
//
        
// When the Finish button is clicked, hydrate the Contact
        
// object with page data and return to our caller, which is
        
// ContactDetailPage1 page function.
        
//
        private void OnFinishClick(object sender, RoutedEventArgs e)
        {
            Contact c
= new Contact();
            c.HomeAddress
= this.txtHomeAddress.Text;
            c.BusinessAddress
= this.txtBusinessAddress.Text;
            OnReturn(
new ReturnEventArgs<object>(c));
            
// close the wizard
            Window w = Application.Current.Properties["AddContactWizard"]
                            
as Window;
            w.Close();

        }
    }
}

  10.到此为止第二个功能页面就完全定义好了。我们把它们和第一个页面上的返回事件处理函数和Next按钮click处理函数绑定到一起。在第一个页面的代码文件ContactDetailPage1.xaml.cs里添加如下代码:

        //
        
// On clicking the next button on PF1, navigate to PF2
        
//
        private void OnNextClick(object sender, RoutedEventArgs e)
        {

            NavigationService ns
=
                         NavigationService.GetNavigationService(
this);
            ContactDetailPage2 pageFunction
= new ContactDetailPage2();
            pageFunction.Return
+= new
                         ReturnEventHandler
<Object>(pageFunction2_Return);
            ns.Navigate(pageFunction);
        }

        
//
        
// When the second PF returns, get the Contact object it
        
// sent back and add the first PF’s data to it
        
// Then return to the UILessPageFunction that called this
        
// ContactDetailPage1 PF.
        
//
        void pageFunction2_Return(object sender, ReturnEventArgs<object> e)
        {
            Contact c
= e.Result as 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));
        }

  11.当联系人列表当中的某一项被选定的时候,我们要把联系人详细信息显示在右边的面板。下面我么就来写这一段代码。我们不是曾经在MainWindow.xaml.cs里面创建了一个空的ListItemSelected方法吗?现在我们把它实现吧:

       //
        
// Triggered when an item in the Contacts list is selected
        
//
        void ListItemSelected(object sender, SelectionChangedEventArgs args)
        {
            
// show first page function on the right hand side frame
            ContactDetailPage1 pageFunction =
                  
new ContactDetailPage1(false, allContacts.SelectedIndex);
            pageFunction.Return
+= new
                   System.Windows.Navigation.ReturnEventHandler
<object>
                            (pageFunction0_Return);
            
this.Frame_RightPane.Navigate(pageFunction);
            
            
//update status bar
            ContactList cl =
                  Application.Current.Properties[
"ContactList"]
                              
as ContactList;
            Contact c
= cl[allContacts.SelectedIndex];
            
this.tb.Text = c.FirstName + " " + c.LastName;
        }

  因为ContactDetailPage1对话框是以只读模式显示的,它不应该返回任何值。我们就在MainWindow.xaml.cs里创建一个空的pageFunction0_Return,好让程序能顺利编译。

0
相关文章