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)
{
PageFunction apage = new PageFunction();
apage.Return += new ReturnEventHandler<object>(apage_Return);
NavigationService.Navigate(apage);
}
private void apage_Return(object sender, ReturnEventArgs<Object> e)
{
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)
{
List<string> selectedItemsList = new List<string>();
foreach (ListBoxItem listItem in listBox2.Items)
{
selectedItemsList.Add(listItem.Content.ToString());
}
ReturnEventArgs<object> returnObject =
new ReturnEventArgs<object>((object)selectedItemsList);
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.
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.