技术开发 频道

实际操作WPF:将数据绑定到对象集合

  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);
        }
    }
}

  在UILessPageFunction.xaml页面里,注册OnLoaded事件处理函数:

<PageFunction
    
x:Class="AddressBook.UILessPageFunction"  
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys
="clr-namespace:System;assembly=mscorlib"
    x:TypeArguments
="sys:Object"
    Loaded
="OnLoaded"
    Title
="UILessPageFunction">
</PageFunction>

  7. 在Read模式情况下,我们想要email地址和主页URL这两项显示成按钮。点击email地址这个按钮应该打开默认的email应用程序,点击主页URL的按钮应该在Frame_RightPane里显示主页的内容。我们创建两个URI,在按钮的事件处理函数里,只要简单地导航到这两个URI就可以了。我们在ContactDetailPage1.xaml.cs里修改代码:

        private void NavigateToHome(object sender, RoutedEventArgs e)
        {
            
if (this.txtHomePage != null)
            {
                NavigationService ns
=
                        NavigationService.GetNavigationService(
this);
                ns.Navigate(
new Uri(this.txtHomePage.Text));
            }
        }

        
private void SendEmail(object sender, RoutedEventArgs e)
        {
            
if (this.txtEmailAddress != null)
            {
                NavigationService ns
=
                            NavigationService.GetNavigationService(
this);
                ns.Navigate(
new Uri("mailto:" + this.txtEmailAddress.Text));
            }
        }

 

0
相关文章