打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to use PageFunction to create dialog behavior in WPF?
How to use PageFunction to create dialog behavior in WPF?

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:

<Grid DockPanel.Dock="Top">

      <Grid.ColumnDefinitions>

            <ColumnDefinition Width="0.3*" />

            <ColumnDefinition Width="0.4*" />

            <ColumnDefinition Width="0.3*" />

      </Grid.ColumnDefinitions>                

      <TextBlock Grid.Row="0" Grid.Column="0">Selected Name:</TextBlock>

      <TextBox Grid.Row="0" Grid.Column="1" Name="txtResult" ></TextBox>

      <TextBlock  Grid.Row="0" Grid.Column="2">

            <Hyperlink Click="SelectHyperlink_Click">Search</Hyperlink>

      </TextBlock>

</Grid>

 

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:

<PageFunction

    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">

     <Grid DockPanel.Dock="Top" Name="grid2" >

      <Grid.ColumnDefinitions>

            <ColumnDefinition Width="0.1*" SharedSizeGroup="Number"/>

            <ColumnDefinition Width="0.1*" SharedSizeGroup="Name"/>

            <ColumnDefinition Width="0.1*" SharedSizeGroup="StartDate"/>

            <ColumnDefinition Width="0.13*" SharedSizeGroup="EndDate"/>

      </Grid.ColumnDefinitions>

      <Grid.RowDefinitions>

            <RowDefinition Height="0.2*" />

            <RowDefinition Height="0.2*" />

            <RowDefinition Height="0.2*" />

            <RowDefinition Height="0.2*" />

            <RowDefinition Height="0.2*" />

            <RowDefinition Height="0.2*" />

      </Grid.RowDefinitions>

      <TextBlock Grid.Column="0" Grid.Row="0">

         <Hyperlink Click="okButton_Click"  Tag=”Value1”>Value1</Hyperlink>

      </TextBlock>

      <TextBlock Grid.Column="1" Grid.Row="0">Value2</TextBlock>

      <TextBlock Grid.Column="2" Grid.Row="0">Value3</TextBlock>

      <TextBlock Grid.Column="3" Grid.Row="0">Value3</TextBlock>

</Grid>

 

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>((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<string> 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!!!!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF调用线程
WPF中的ControlTemplate(控件模板)
Windows Phone新手开发教程(二)
【WPF学习】第四十章 画刷
利用C#动态生成Xaml
WPF简单导航框架(Window与Page互相调用)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服