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:
- “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.
- “KeepAlive”: When this property is set to true, an instance of the page is retained in the navigation history.