Silverlight 2 UserControl base class in Blend |
|
I have been doing a lot of Silverlight2 development the last few months. And one of the things you will start to do pretty quickly is make base classes for your pages. You can do this by creating a base class which inherits a UserControl, inherit from it in your Page.xaml.cs and then changing the root of your XAML from <UserControl to <local:MyPageBase. Like this:
Page.xaml.cs:
public partial class Page : PageBase
{
public Page()
{
InitializeComponent();
}
}
PageBase.cs: (In another "common" project)
public class PageBase : UserControl
{
}
Page.xaml:
<common:PageBase x:Class="Silverlight2UserControlWithBaseClassBlendFix.Client.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:Silverlight2UserControlWithBaseClassBlendFix.Client.Common;assembly=Silverlight2UserControlWithBaseClassBlendFix.Client.Common">
<Grid x:Name="LayoutRoot"/>
</common:PageBase>
The blend catch
However there is one catch to this whole exercise: you lose editing capabilities in blend!
This is of course something that will be fixed in the final version, but for now it is a breaking bug. Which "almost" makes you run away from base classes.
I have seen a lot of people complain about the bug, but I have not found anyone giving at least a temporary solution until it is fixed.
Not so much a solution
First I thought about a very simple solution. To use the page only as a container and put all the content in another user control. But this has the disadvantage that you loose all the functionality from the base class again, and that was was we were aiming for. So it solves one problem and we get back the one we had.
At least it works
First couple of weeks, I just stuck with it, no base classes. I used extension methods and other "not so much solutions" to work around the problem. But today I finally had enough and started on a bandage that should hold at least until the final versions are released.
The idea is to use a text SearchAndReplace tool in the pre build event, to replace <UserControl to <local:BasePage and back again in the post build.
I had already written a simple command line SeardAndReplace tool before so this was ideal for the job. It does gives you random build errors, but this is simply resolved by building your project again.
Pre-Build: (replace <UserControl to <common:PageBase )
$(SolutionDir)SearchAndReplace $(ProjectDir)Page.xaml /search="<UserControl " /replace="<common:PageBase "
$(SolutionDir)SearchAndReplace $(ProjectDir)Page.xaml /search="</UserControl>" /replace="</common:PageBase>"
Post-Build: (replace back to <UserControl)
$(SolutionDir)SearchAndReplace $(ProjectDir)Page.xaml /search="<common:PageBase " /replace="<UserControl "
$(SolutionDir)SearchAndReplace $(ProjectDir)Page.xaml /search="</common:PageBase>" /replace="</UserControl>"
Click here to download the whole "proof of concept project".
Stay in the light!
Robertjan Tuit
Categories : Silverlight, C#














RSS English
RSS Nederlands





