Click here to Skip to main content
Click here to Skip to main content

WPF PageFunction Object

, 27 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the use of the WPF PageFunction object in Page based WPF applications.

Introduction

This article demonstrates the use of the WPF PageFunction object. The PageFunction object plays an important role in Page based WPF applications. The PageFunction class is almost similar to the Page class in WPF. You can design a PageFunction in XAML, and any WPF control can be added to the PageFunction object.

Background

We will look at the WPF PageFunction object basics, like what the PageFunction object is, how the PageFunction object is different from the Page object, and how to make use of the PageFunction object.

The PageFunction class is a derived version of the Page class. Navigation service or hyperlinks can be used to navigate a PageFunction. The only thing that makes a PageFunction different from a Page object is it can return a value to the caller. This is like in Windows Forms applications where you show a dialog box to the user to collect some data and return back to the caller window. PageFunction can return any value, i.e., any .NET object like an integer, string or a custom object. By default, Visual Studio adds a String type as the return value like shown below, but you can change this to any .NET object you want:

<PageFunction 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:Class="WPF.PageBasedApplication.PageFunction" 
 x:TypeArguments="sys:Object" 
 Title="PageFunction" RemoveFromJournal="True" />

Using the code

The sample project is really straightforward. The source contains a solution with a WPF project targeting the .NET Framework 3.5. The application has a Page (named “HomePage”) and a PageFunction (named “PageFunction”). The application is like a pizza-ordering system. The user navigates from the main menu (i.e., HomePage) to the selection menu (i.e., PageFunction) and allows the user to select toppings and return to the main menu with their selections.

When your PageFunction returns the values, you need to call the “OnReturn” method. The “OnReturn” method takes a type parameter specified for PageFunction. You can pass a null value as a parameter if you are not expecting any return value. In addition to this, the caller (the page that calls the PageFunction) has to handle the “Return” event for that PageFucntion, and an instance of the “ReturnEventAgrs” returned by that event will contain the returned value.

The home page calls the PageFunction on the click of a button, and at the same time, attaches the “Return” event handler to get the value returned by PageFunction. Shown below is a code example:

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Create instance of PageFunction 
    PageFunction apage = new PageFunction();
    //Attach the Return EventHandler to Handle 
    //ReturnEventAgrs passed from PageFunction 
    apage.Return += new ReturnEventHandler<object>(apage_Return);
    //Navigate to Page Function 
    NavigationService.Navigate(apage);
}

/// <summary>
/// Return EventHanlder Method to ReturnEventAgrs passed from PageFunction
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void apage_Return(object sender, ReturnEventArgs<Object> e)
{
    //Display the Items selected
    List<string> selectedItems = (List<string>)e.Result;

    foreach (string item in selectedItems)
    {
       listBox1.Items.Add(item);
    }
}

Now, let’s see how the PageFunction returns values to the caller. Once you are ready to return a value to the caller from a PageFunction, call the “OnReturn” method and pass the return value in a new instance of “ReturnEventArgs”. Shown below is a code example:

private void Button3_Click(object sender, RoutedEventArgs e)
{
    //Collect the selected items in List
    List<string> selectedItemsList = new List<string>();

    foreach (ListBoxItem listItem in listBox2.Items)
    {
    selectedItemsList.Add(listItem.Content.ToString());
    }

   //Create instance of ReturnEventArgs to pass data back to caller page
   ReturnEventArgs<object> returnObject = 
      new ReturnEventArgs<object>((object)selectedItemsList);

   //Call to PageFunction's OnReturn method and pass selected List
   OnReturn(returnObject);
}

Other Important Properties of PageFunction:

  1. RemoveFromJournal”: When this property is set to true, it automatically deletes the entry from the Journal once your task is completed so the user won’t get a PageFunction during navigation.
  2. KeepAlive”: When this property is set to true, an instance of the page is retained in the navigation history.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Saki Sachin
Software Developer (Senior)
United States United States
Sachin has been developing software professionally for more than five years. Mostly working with .NET Framework 3.5/3.0/2.0/1.x, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF)\Smart Client, ASP.NET 3.5/2.0/1.x (WebForm), XAML, ASP.NET AJAX 1.0, C# (3.0), LINQ, VB.NET, ADO.NET, WinForm, Web Services, MS Sync Framework, Window Services, UI Automation Framework, SQL Server and good experience in agile/scrum development methodology.

Comments and Discussions

 
QuestionSeems convoluted. What are the advantages Pinmemberwhiteheadw9-Jan-13 11:11 
GeneralThanks Sachin Pinmembervishalkumarsinha8-Jun-12 22:07 
GeneralThanks Sachin! Pinmemberachchu21-Oct-10 7:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 27 Feb 2009
Article Copyright 2009 by Saki Sachin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid