How to use PageFunction to create dialog behavior in WPF?
来源:百度文库 编辑:神马文学网 时间:2024/04/29 07:11:12
PageFunctionis a new term defined in WPF. It enables the user to navigate to aspecific page and perform a task, then navigate back to the caller pagewith the result. It behaves just like Modal Dialogbox with thedifference that PageFunction won’t be displayed as s pop-up, instead itis displayed in the same page as the caller.
Theother nice feature of PageFunction is that it won’t be included in thenavigation history, making navigation process much simpler for both theuser and the developer.
Nowwe need to come up with a scenario that the PageFunction would makesense. Let’s say you need to have a search page that the user cannavigate to by clicking on a link, after the user found his/her itemthen it would navigate back to the main page with the search resultdisplayed in a textbox. This is very common scenario.
This is what you need first, a textbox and a link in the main page that navigates to the search page:
Selected Name:
Search
Wewill work on the event handler “SelectHyperlink_Click” later. So farnothing new… Now you need to define your search page as a PageFunctionin order to meet your requirement. So this is how you would implementyour search page:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MSLWPFPrototype.SearchPageFunction"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="sys:String"
Title="OrganizationSearchPageFunction">
Value1
Value2
Value3
Value3
The only thing interesting here in this code is:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="sys:String"
Thefirst line is to specify that you want to use .Net type like stringhere in XMAL file and the TypeArguments is the type of the object thatthe PageFunction returns back to the caller page.
You also need to implement these two event handler as part of your PageFunction:
void okButton_Click(object sender, RoutedEventArgs e)
{
// Accept task when Ok button is clicked
Hyperlink cmd = (Hyperlink)sender;
OnReturn(new ReturnEventArgs((string)cmd.Tag));
}
void cancelButton_Click(object sender, RoutedEventArgs e)
{
// Cancel task
OnReturn(null);
}
Thatis it for the PageFunction, not that difficult!!! Now you need to addcouple more event handlers to your caller page to accept the result anddisplay it in the textbox:
void taskPageFunction_Return(object sender, ReturnEventArgs e)
{
if (e == null) return;
// If a task happened, display task data
this.txtResult.Text = e.Result;
}
And the event handler for the link to navigate to the Search page:
void SelectHyperlink_Click(object sender, RoutedEventArgs e)
{
// Instantiate and navigate to task page function
SearchPageFunction taskPageFunction = new SearchPageFunction("Initial Data Item Value");
taskPageFunction.Return += taskPageFunction_Return;
this.NavigationService.Navigate(taskPageFunction);
}
Done, enjoy your Search PageFunction!!!!
Theother nice feature of PageFunction is that it won’t be included in thenavigation history, making navigation process much simpler for both theuser and the developer.
Nowwe need to come up with a scenario that the PageFunction would makesense. Let’s say you need to have a search page that the user cannavigate to by clicking on a link, after the user found his/her itemthen it would navigate back to the main page with the search resultdisplayed in a textbox. This is very common scenario.
This is what you need first, a textbox and a link in the main page that navigates to the search page:
Wewill work on the event handler “SelectHyperlink_Click” later. So farnothing new… Now you need to define your search page as a PageFunctionin order to meet your requirement. So this is how you would implementyour search page:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MSLWPFPrototype.SearchPageFunction"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="sys:String"
Title="OrganizationSearchPageFunction">
The only thing interesting here in this code is:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:TypeArguments="sys:String"
Thefirst line is to specify that you want to use .Net type like stringhere in XMAL file and the TypeArguments is the type of the object thatthe PageFunction returns back to the caller page.
You also need to implement these two event handler as part of your PageFunction:
void okButton_Click(object sender, RoutedEventArgs e)
{
// Accept task when Ok button is clicked
Hyperlink cmd = (Hyperlink)sender;
OnReturn(new ReturnEventArgs
}
void cancelButton_Click(object sender, RoutedEventArgs e)
{
// Cancel task
OnReturn(null);
}
Thatis it for the PageFunction, not that difficult!!! Now you need to addcouple more event handlers to your caller page to accept the result anddisplay it in the textbox:
void taskPageFunction_Return(object sender, ReturnEventArgs
{
if (e == null) return;
// If a task happened, display task data
this.txtResult.Text = e.Result;
}
And the event handler for the link to navigate to the Search page:
void SelectHyperlink_Click(object sender, RoutedEventArgs e)
{
// Instantiate and navigate to task page function
SearchPageFunction taskPageFunction = new SearchPageFunction("Initial Data Item Value");
taskPageFunction.Return += taskPageFunction_Return;
this.NavigationService.Navigate(taskPageFunction);
}
Done, enjoy your Search PageFunction!!!!
How to use PageFunction to create dialog behavior in WPF?
how to use raid in linux
How to define a DataGrid using ListView in WPF?
How to write a Scrollable Data Grid in WPF?
How To: Use Digital Storytelling in Your Classroom
How To Use Speech Recognition in Windows XP
How to Use SQLite in Ruby on Rails
Slideshows In Web Design: When And How To Use Them
How to Use Retro Colors in Your Designs
How to use RSS feed
how to use "gonna" & "gotta"
ExcelADO demonstrates how to use ADO to read and write data in Excel workbooks
How to Create an HTML Editor Application
When to use “on” and when to use “in”
How to add a column in a Listview dynamically using WPF
how to use C Vectors ? - CGDTech - CSDNBlog
How to use this HD Converter?
how to use C Vectors ? - CGDTech - CSDNBlog
How to use DSP caches, part 1
How to Add Properties to Custom WPF Control
How To Create A RSS Feed From Any Web Page
XML.com: How to Create a REST Protocol
How to Create a Clean Blog Design with Photoshop
Visual C# .NET: How to Create a Class Library