PageFunctions are very interesting concept of WPF. If you want to use some kind of a navigation in your applications you will definitely have a scenario where you want to redirect the user to a page where to ask for some input and after that receive that in the previous page. This is where PageFunction can be used. It works the same as a normal functions. You call a function passing some parameters and expect a specific type of result. PageFunction<T> essentially allows you to treat a page navigation like a function call, in which a page navigates to (calls) a page function and expect a return result.
Though it is ok to use these types of functions in Visual Studio 2008, in Expression Blend you receive these errors when you try to open a PageFunction in the designer:
The name "PageFunction" does not exist in the namespace
"http://schemas.microsoft.com/winfx/2006/xaml/presentation".
The property "TypeArguments" does not exist in the
"http://schemas.microsoft.com/winfx/2006/xaml" namespace.
Recently Microsoft released Expression Blend 1 SP1 and Blend 2 December Preview. I was curious if these new builds supports page functions and I have posted a question on the forums (link). Interestingly enough I received answers that PageFunctions are not even supported in Blend v2. WHAT THE...? Man I can't wait till v4. I guess many of you either. One thing caught my eye on the experts answers:
No, we don't plan to support generics at the root of markup (of which
PageFunction<T> is an example) in version 2 either.
Well no support for generics at the root of xaml markup. If this is the problem can we workaround it? That's the million dollar question don't you think. The answer is YES we can. Below I will show you how.
First you need to have a PageFunction in your project. If you do not have one use Visual Studio and Add New Item to add new PageFunction with meaningful name. This is how xaml looks like by default. Expression Blend have a problem with this "x:TypeArguments" attribute and the with the root element "PageFunction"
<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="UntitledProject1.SelectUserNamePageFunction"
x:TypeArguments="sys:String"
Title="SelectUserNamePageFunction">
<Grid>
</Grid>
</PageFunction>
Depending on what name you choused for the page function you will have a code behind class created for it. I have SelectUserNamePageFunction.xaml.cs file which look like
namespace UntitledProject1
{
/// <summary>
/// Interaction logic for SelectUserNamePageFunction.xaml
/// </summary>
public partial class SelectUserNamePageFunction : SelectUserNamePageFuncitonBase
{
public SelectUserNamePageFunction()
{
InitializeComponent();
}
}
}
What we need to do is add one more class in the hierarchy and make SelectUserNamePageFunction derive from it. Just add a SelectUserNamePageFunctionBase class below the one that we have already created. This will simplify its removal after we have proper PageFunction support in future Blend versions. It is very important to have the SelectUserNamePageFunction class at the top the the namespace since the Visual Studio WPF Designer (Cider) will not be able to add event handlers and will complain about it.
namespace UntitledProject1
{
/// <summary>
/// Interaction logic for SelectUserNamePageFunction.xaml
/// </summary>
public partial class SelectUserNamePageFunction : SelectUserNamePageFuncitonBase
{
public SelectUserNamePageFunction()
{
InitializeComponent();
}
} public class SelectUserNamePageFuncitonBase : PageFunction<string> { }
}
Select the SelectUserNamePageFunction.xaml file in Solution Explorer and change the Custom Tool Namespace property to "UntitledProject1" since this is our namespace.
Change the xaml file. Add the crl-namespace:UntitledProject named z. Change the xaml root element to z:SelectUserNamePageFuncitonBase. The final file look like:
<z:SelectUserNamePageFuncitonBase
xmlns:z="clr-namespace:UntitledProject1"
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="UntitledProject1.SelectUserNamePageFunction"
Title="SelectUserNamePageFunction">
<Grid>
</Grid>
</z:SelectUserNamePageFuncitonBase>
That's all. Now you can open this SelectUserNamePageFunciton.xaml in Blend and edit it just like any other Page.
Happy coding!
Currently rated 5.0 by 2 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5