Plip's Weblog

Phil Winstanley - British Microsoft ASP.NET MVP & ASP Insider.

XAML from ASP.NET - It's Magic - Burn him - He's a Witch!

Today I decided to set myself a little challenge, I wanted to see if I could use the Control Adapter framework which shipped with ASP.NET 2.0 that allows the developer to override and uniquely control the rendering of all the ASP.NET controls, to render and ASP.NET page in normal HTML for IE and in XAML for Firefox.

 

This little project took me three hours to put together and get to the stage where I can write about my experience, so it’s a bit rough and ready!

 

SOURCE CODE CAN BE FOUND HERE

http://www.myservicescentral.com/CodeSamples/CssAdapters1.zip

 

A little background before we move on, I’ve not really used XAML yet, but understand it will be the markup for future Windows Forms style applications and will force all those Windows Developer to learn how to use <spikey brackets />. A friend of mine has been ranting on for years about how ASP.NET will be the platform of the future and how it will render out content as it see’s fit, with that in mind, I thought I’d take a stab at producing XAML from ASP.NET controls.

 

The first thing I did was checked out this post from Scott Guthrie on the ASP.NET 2.0 Control Adapter Architecture. Then I grabbed a copy of the CSS Adapter Toolkit, which details how to create CSS friendly controls and gives some really cool examples of some of the ASP.NET controls which have already been CSSified (is that a word? Well it is now!).

 

After reading through the source code which is all provided with the CSS Adapter Toolkit I was SCARED at how EASY it all was!

 

The fist thing I did was create a page which I could use to test my XAML Adapters.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XamlPage.aspx.cs" Inherits="XamlPage" %>

<form runat="server">

 

<p>Here is some content</p>

 

<asp:Label id="EmailLabel" runat="server" Text="What is your e-mail address?"></asp:Label>

 

<asp:TextBox id="EmailTextBox" runat="server" Text="Type your e-mail address here." />

 

<asp:RequiredFieldValidator id="EmailRequired" runat="server" ControlToValidate="EmailTextBox" ErrorMessage="Email Address is Required" />

 

<asp:Button id="EmailButton" runat="server" Text="Register your e-mail address!" />

 

</form>

 

Once I had my page it was time to build my first Adapter so I set to it at the top level and decided to create my own System.Web.UI.Page implementation which instead of squirting out something like <html><head /><body></body></html> needed to produce the XAML equivalent which is <application></application>. The code was amazingly simple: -

 

using System;

using System.Web;

using System.Web.UI;

 

namespace Plipster.XamlAdapters

{

    public class PageXamlAdapter : System.Web.UI.Adapters.PageAdapter

    {

        protected override void Render(HtmlTextWriter writer)

        {

 

            writer.WriteLine();

            writer.WriteBeginTag("Application");

            writer.Write(">");

            writer.WriteLine();

 

            base.Render(writer);

 

            writer.WriteLine();

            writer.WriteEndTag("Application");

 

        }

 

        public PageXamlAdapter()

        {

        }

    }

 

}

 

Once the control had been created I needed to tell ASP.NET to use my new control in place of the standard System.Web.UI.Page control under the circumstances that I wanted it to. In the Real World those circumstances would be “The client is running WinFX and can support XAML”, in the world of test environments it was “The client is running Firefox so we’ll pretend that’s WinFX”.

 

I created this browser file: -

 

<browsers>

  <browser refID="MozillaFirefox">

    <controlAdapters>

      <adapter controlType="System.Web.UI.Page"             adapterType="Plipster.XamlAdapters.PageXamlAdapter" />

    </controlAdapters>

  </browser>

</browsers>

 

So once this is all set up, requesting any ASP.NET page through firefox just returns the following content: -

 

<Application>
</Application>

 

Extend it a little bit and create all the Controls that I listed in the page with their XAML equivalents and you get something like this: -

 

<Application>
<Canvas Background="LightCyan">
<Text>
<p>Here is some content</p>
</Text>
<Text>What is your e-mail address?</Text>
<TextBox>Type your e-mail address here.</TextBox>
<Text Color="Red">Email Address is Required</Text>
<Button>Register your e-mail address!</Button>
</Canvas>
</Application>

 

 

So what’s so cool about this? Well it shows the true power of control adapters, it also shows how easy they are to manipulate and bend to your own will.

 

Next steps: -

 

  1. How do you get code (C#/VB.NET) to work with this XAML?
    1. Do we go about rendering and compiling on the server and then stream down a binary with all the code pulled together on the server and compiled?
    2. Perhaps some form of Ajax style abstraction layer such as Atlas will communicate from the client to the server – maybe even use Atlas and XMLScript to do this?
  2. What else can we produce, not just XAML and HTML, surely there are other markups or text based languages we can produce with the adapters.
    1. What about a Java Adapter, one which produces client runnable applications that can be created to run on non Windows Machines?
    2. Mobile devices – what a huge topic, and exercise for the reader I think!
    3. How cool would a Windows Forms Adapter be – how about a bit of code generation, server side compilation and then binary stream of an executable down to a client – it’s just text after all! Here’s the Windows Forms code for a similar UI to the one ASP.NET produced.

 

private void InitializeComponent()

{

      this.label1 = new System.Windows.Forms.Label();

      this.EmailLabel = new System.Windows.Forms.Label();

      this.EmailTextBox = new System.Windows.Forms.TextBox();

      this.EmailButton = new System.Windows.Forms.Button();

      this.SuspendLayout();

      //

      // label1

      //

      this.label1.Location = new System.Drawing.Point(16, 40);

      this.label1.Name = "label1";

      this.label1.Size = new System.Drawing.Size(120, 23);

      this.label1.TabIndex = 0;

      this.label1.Text = "Here is some content";

      //

      // EmailLabel

      //

      this.EmailLabel.Location = new System.Drawing.Point(24, 96);

      this.EmailLabel.Name = "EmailLabel";

      this.EmailLabel.Size = new System.Drawing.Size(160, 23);

      this.EmailLabel.TabIndex = 1;

      this.EmailLabel.Text = "What is your e-mail address?";

      //

      // EmailTextBox

      //

      this.EmailTextBox.Location = new System.Drawing.Point(208, 96);

      this.EmailTextBox.Name = "EmailTextBox";

      this.EmailTextBox.Size = new System.Drawing.Size(136, 20);

      this.EmailTextBox.TabIndex = 2;

      this.EmailTextBox.Text = "Type your e-mail address";

      //

      // EmailButton

      //

      this.EmailButton.Location = new System.Drawing.Point(24, 144);

      this.EmailButton.Name = "EmailButton";

      this.EmailButton.Size = new System.Drawing.Size(176, 23);

      this.EmailButton.TabIndex = 3;

      this.EmailButton.Text = "Register Your e-mail address";

      //

      // Form1

      //

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.ClientSize = new System.Drawing.Size(400, 238);

      this.Controls.AddRange(new System.Windows.Forms.Control[] {

                                                                                                      this.EmailButton,

                                                                                                      this.EmailTextBox,

                                                                                                      this.EmailLabel,

                                                                                                      this.label1});

      this.Name = "Form1";

      this.Text = "Form1";

      this.ResumeLayout(false);

 

}

 

 

Okay I think that’s enough food for thought – play nicely now children J

Posted: Apr 29 2006, 05:57 PM by Plip | with 23 comment(s)
Filed under:

Comments

Paul D. Murphy said:

nicely done.

the more i've thought about this today, however, the more i think the problem is a long way from being solved.

currently the working model would be like you present. i.e. use the asp.net adapter model to 'transform' some kind of form abstraction model into some kind of ui model. that works.

the problem is that the asp.net abstraction model has a bunch of funk baked into the abstractions that aren't so abstract (the non-adapter control code).

fixing this would require a siginifigant refactoring of the current system.web.ui namespace to push as much of the rendering code out into a set of adapters for html.

until you do that the object model is fundamentally broken.

consider this...

---------------------------------------
| System.Web.UI (Key Abstractions) |
| Control, etc... these target |
| only System.Forms.I* |
| and render nothing. adapters |
| do all that work |
---------------------------------------
| System.Forms (Interface Layer) |
| ITextControl, IButtonControl |
| IContainerControl |
---------------------------------------

The current rendering code could then be shuffled out into a set of html rendering adapters that could live in companion namespaces with the xaml adapters, winfowm adapters, and any other future UI paradigms that we would represent with a transformation from the baseline System.Forms markers and System.Web.UI logic controlllers.

thoughts?
# April 29, 2006 1:17 PM

CRAZY FRIEND said:

HOW DARE YOU BREAK MY TRUST..... I FIRE LASER AT YOU... PEW PEW PEW....
# April 29, 2006 1:19 PM

Phil Winstanley said:

Another thought which has occured to me is the integration of ASP.NET Themes and Skins to allow the addition of client specific design surface support - for example, Windows Forms uses absolute positioning, Browsers have a flow design (as does XAML but that supports both).

If those numbers could be specified in a Skin file and interpreted correctly by the control adapters, you'd have designability in there too.
# April 29, 2006 1:23 PM

Paul D. Murphy said:

i just took a pretty lengthy walk and this jostled around in my head most of the way. you mentioned this on messenger before i stepped out so it was on my mind.

i think the same model would work for the style and layout components of the system. style is implmented (poorly imo more later on this) in the system.web.ui namespace already. layout is a construct that is approached with the various panel controls, but the abstractions are just not rich enough to support all models (winforms)

however, you could fix all of that by taking the same basic approach as above. create markers in a system.forms namespace that address the common layout and style elements (probably needs to be something here for mapping event handlers as well). create baseline logic controllers in the system.web.ui namespace and finally implement the ui transformation inside the technology specific adapter namespace.

i only hope microsoft does something like this. the model is very powerful and extensible for future paradigms.
# April 29, 2006 2:18 PM

Dave said:

This is a very very cool idea...

I think it works reasonably as a little tech demo like you have here but how many real world web apps are so pure? You have css (for fundamental layout as well as style), javascript and all sorts of other stuff which make up an application and it just isnt going to translate.

This doesnt take away from what you have done in any way, it is very nicely done and serves as an excellent example of what can be done with control adapters.
# April 29, 2006 2:45 PM

James Knowles said:

Windows Forms generation might be easier in Vista because I believe you can use a style of markup to generate pages. .. methinks me might go have a look.
# April 30, 2006 6:09 AM

Peter said:

I've been suspicious about you for some time, Plip... but you know what, we love you anyway.

So between you and Paul, server object model and client compatibility... anyone got a best guess as to when this will be viable?
# May 1, 2006 10:58 AM

Richard Costall said:

Great one plip,

I was talking about this in the car the other day - creating a 'converter' from asp.net to WPF. You solution is pretty slick too - cool.


Rich
# May 5, 2006 2:42 AM

hgf said:

njgfhg
# May 10, 2006 4:08 AM

d said:

ddd
# May 10, 2006 9:15 PM

Alex said:

This article has given me a bit of food for thought and was wondering if this could used with the Model View Presenter (MVP) design pattern?  

Whereby you specify your UI using an XAML type format which in turn is used to retrieve values from a View by your Presenter layer?

Or, am I barking up the wrong tree entirely...

# February 22, 2007 6:19 AM

silver said:

<a href=http://kong.web-skill.org/free-adventure-online-naruto-game/> free adventure online naruto game</a>|

<a href=http://kong.web-skill.org/masculines-nues-star/> masculines nues star</a>|

<a href=http://kong.web-skill.org/indian-cooking-recipe-for-dal/> indian cooking recipe for dal</a>|

<a href=http://kong.web-skill.org/weight-watcher-cookbook/> weight watcher cookbook</a>|

<a href=http://kong.web-skill.org/warehouse-club-superstores/warehouse club superstores</a>|

<a href=http://kong.web-skill.org/herbalife-distributor/> herbalife distributor</a>|

<a href=http://kong.web-skill.org/sweet-potato-ethanol/> sweet potato ethanol</a>|

<a href=http://kong.web-skill.org/automotive-gasket-material/> automotive gasket material</a>|

<a href=http://kong.web-skill.org/taquilla-key-upload/> taquilla key upload</a>|

<a href=http://kong.web-skill.org/michelle-vieth-sex-video/> michelle vieth sex video</a>|

<a href=http://kong.web-skill.org/food-to-avoid-while-on-coumadin/> food to avoid while on coumadin</a>|

<a href=http://kong.web-skill.org/erotic-hypnosis-video/> erotic hypnosis video</a>|

<a href=http://kong.web-skill.org/madonna-desnuda/> madonna desnuda</a>|

<a href=http://kong.web-skill.org/bam-margera-skateboard/> bam margera skateboard</a>|

<a href=http://kong.web-skill.org/dreamcast-iso-rapidshare/> dreamcast iso rapidshare</a>|

<a href=http://kong.web-skill.org/bbc-co-uk-food-recipe/> bbc co uk food recipe</a>|

<a href=http://kong.web-skill.org/slime-ball-soccer/> slime ball soccer</a>|

<a href=http://kong.web-skill.org/maine-coon-cat/> maine coon cat</a>|

<a href=http://kong.web-skill.org/anais-desnuda/> anais desnuda</a>|

<a href=http://kong.web-skill.org/michelle-vieth/> michelle vieth</a>|

# April 8, 2007 3:34 PM

shajipd said:

Hi

I am not able to download  the source code from location mentioned

www.myservicescentral.com/.../CssAdapters1.zip

Could any one mail me the source code zip.

thanks in advance

# May 31, 2007 4:27 AM

subbu said:

hi, I want how write code front end to back end connection using xaml 3.0. if u know give me reply

subbu07@gmail.com

# February 20, 2008 2:03 AM

greg said:

Yes, you are right.

# March 17, 2008 11:32 PM

easy ways to make money said:

I found your site on faves.com bookmarking site.. I like it ..gave it a fave for you..ill be checking back later

# September 15, 2008 3:16 PM

make money quick said:

Pages About August 2008 S M T W T F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Archives August 2008 Blogroll WordPress. com WordPress. org Meta Log in Entries RSS Comments RSS WordPress. org Search for: Recent

# September 17, 2008 3:41 PM

Calciu Sorin said:

Hey guys i have succeded to generate XAML images through ASP .NET. See coolrgb.com as an example

# September 21, 2008 10:07 AM

online publicity said:

As for coursework, there were some great classes about teaching writing (featuring many of the writers mentioned in the introduction to this blog entry), and an extraordinary course entitled“ Applied Grammar and Usage for Writers,” which despite that

# October 19, 2008 12:37 AM

gramakri said:

I am not able to download the zip file from the location

www.myservicescentral.com/.../CssAdapters1.zip

Has anyone downloaded it before?

# November 7, 2008 2:07 AM

Horward Huges said:

Nice work dude a great effort to be wished

# May 11, 2009 3:28 AM

SouthWind17 said:

Next parachutes come out, causing even more air resistance to slow the lander down. ,

# October 22, 2009 1:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)