Click here to Skip to main content
15,867,885 members
Articles / Desktop Programming / WPF

WPF PageFunction Object

Rate me:
Please Sign up or sign in to vote.
3.63/5 (5 votes)
27 Feb 2009CPOL2 min read 45.9K   1.4K   9   3
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:

XML
<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:

C#
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:

C#
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)


Written By
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 Pin
whiteheadw9-Jan-13 11:11
whiteheadw9-Jan-13 11:11 
GeneralThanks Sachin Pin
vishalkumarsinha8-Jun-12 22:07
vishalkumarsinha8-Jun-12 22:07 
GeneralThanks Sachin! Pin
achchu21-Oct-10 7:56
achchu21-Oct-10 7:56 

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

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