Lubo Blagoev's Blog

My thoughts on software and technology

Workaround for using WPF PageFunctions in Expression Blend


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.
 
 image_2
 
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.
 
Download the sample solution UntitledProject1.zip

Happy coding!

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

December 8, 2007 20:03 by lubo
Tags: , ,
Categories:

Comments

May 26. 2009 16:50 by Mustafa YILDIZ

Thanks for the workaround dude, it really helped ^^. Hope new versions of blend will support pagefunctions.

January 14. 2010 19:59 by Benjay

Just for information i don't think that changing the custom tool namespace is required. Having a base class seems to be enough.

Thanks for your tip!

July 13. 2010 15:56 by Shimmy

@Mustafa, merhaba!
I installed Expression Blend 4 and they haven't fix this bug yet.
I posted a connection over here: connect.microsoft.com/.../unable-to-edit-pagefunction-in-expression-blend-4-0-20525-0
Please vote and reproduce.

August 31. 2010 18:18 by jordan retro 1

I admire the beneficial data you present within your posts. I'll bookmark your blog site and also have my youngsters verify up the following typically. I'm really positive they are going to understand plenty of new stuff the following than anybody else!

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]


Live preview

September 6. 2010 21:28 by