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:

Space Shuttle Atlantis headed to the skies on Dec. 6


 STS-122 Atlantis 6.Dec.2008

At 4:31 p.m. EST Space shuttle Atlantis will blast towards space.

If you never followed one of these missions I suggest you do so. Just watch those space walks online at Nasa TV and you will be totally sold out as I was. When I first watched an EVA I was stunned how quiet and peaceful is out there. Beautiful. Just Beautiful. I thought it is so risky and hard to be there in this deadly quite place, but as Barbara Morgan said during STS-118 this is the most productive environment where no cable tv, no gsm, no noise is distracting you from the task you want to do. If you want to prepare yourself with what is going to happen review the STS-122 Mission Overview.

Currently the space station look like this.

 Space Station before STS-122

STS-122 will bring the Columbus module to boost scientific activities on the station. Enough from an amateur like me. If you want to know more watch the most interesting interview I have ever watch by now regarding space station's future. The interview is with a German astronaut Hans Schlegel who will serve as a Mission Specialist-3 on STS-122. Interestingly enough this man is married with seven children. I can just admire the dedication and excitement this man holds of his job.

Be the first to rate this post

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

December 3, 2007 02:30 by lubo
Tags:
Categories: Space Exploration