Welcome to WindowsClient.net | My Blog | Sign in | Join

Rob Relyea - XAMLified

WPF, Silverlight and XAML

Syndication

Sponsors





  • advertise here
Exploring new techniques for DLR Script in XAML

Thursday’s…

PDC09’s XAML Futures talk will show off several demos…one of them will be a demo of .NET 4’s System.Xaml.dll and the DLR.

Code intertwined with markup, generally isn’t best practice, but I’ve talked with several customers who have wanted to enable customization scenarios with markup + script.  BAML and IL didn’t seem to help their scenarios. And XAML by itself doesn’t have ability to have code. There have been .NET 3 explorations of DLR + XAML as well. (DLRPad. Events in compiled and uncompiled scenarios.)

XamlScriptServices.Parse() Explanation

My demo creates a class called XamlScriptServices.  When you call XamlScriptServices.Parse(xamlString), it will give allow you to put Ruby script inside of x:Code, and events. Named elements will be accessible from script.  Events with an leading "[" and a trailing "]" will be treated as Ruby code to wire to the event. Other event handlers will be attempted to be wired to that method on the root object. The demo currently enables one x:Code block per parent element.

Other DLR languages could be chosen instead, the class could even be enhanced to enable specifying the script engine via some attribute in the XAML.

The Markup + Ruby Sample

This is just a technology demo, but I’d *love to hear what you think*…remember this is a tool for some scenarios…not all.

  1. <Page
  2. xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  3. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xml:space='preserve'>
  4.   <Page.Loaded>[<![CDATA[
  5.     tb1.Tag = Car.new('the beach')
  6.   ]]>]</Page.Loaded>
  7.   <Page.LayoutTransform>
  8.     <ScaleTransform ScaleX='3' ScaleY='3' />
  9.   </Page.LayoutTransform>
  10.   <UniformGrid Columns='2' Rows='4'>
  11.     <Button Click='[tb1.Text=tb1.Tag.go()]'>go</Button>
  12.     <Button Click='[tb1.Text=tb1.Tag.stop()]'>stop</Button>
  13.     <Label Target='tb1' >tb1:</Label>
  14.     <TextBox Name='tb1' />
  15.     <Label Target='tb2'>tb2:</Label>
  16.     <TextBox Name='tb2' />
  17.     <Label Target='tb3'>tb3:</Label>
  18.     <TextBox Name='tb3' />
  19.   </UniformGrid>
  20.  
  21.  
  22. <x:Code><![CDATA[
  23. class Car < Object
  24.   attr_accessor :destination
  25.   def initialize(destination)
  26.     @destination = destination
  27.   end
  28.   def go()
  29.     'starting'
  30.   end
  31.   def stop()
  32.     'stopping'
  33.   end
  34. end
  35. ]]></x:Code>
  36. </Page>

(Haven’t figured out how to avoid storing the Car object in tb1.Tag.  Ideally a variable created in Page.Loaded would be available from other script blocks.  Digging more into DLR script hosting at http://dlr.codeplex.com/)

Published Saturday, November 14, 2009 11:32 AM by Rob_Relyea
Filed under: , , , ,

Comments

# re: Exploring new techniques for DLR Script in XAML@ Saturday, November 14, 2009 4:34 PM

Get this horrible idea off the internet.  This goes against every bit of better coding practices developed in the last few years with the rise of separation of concerns via MVC or MVVM patterns.  If you want to play around with untestable, unreadable garbage UI mixed with logic go play with some old ASP or PHP and keep it out of XAML.

by brad

# Twitter Trackbacks for Exploring new techniques for DLR Script in XAML - Rob Relyea - XAMLified [windowsclient.net] on Topsy.com@ Saturday, November 14, 2009 5:20 PM

Pingback from  Twitter Trackbacks for                 Exploring new techniques for DLR Script in XAML - Rob Relyea - XAMLified         [windowsclient.net]        on Topsy.com

# re: Exploring new techniques for DLR Script in XAML@ Saturday, November 14, 2009 9:38 PM

brad-

I've has several customers ask for a non-compilation way to allow customization of applications. I believe script will play a part here.

Did you ignore my disclaimers?

Wow...

-Rob

# re: Exploring new techniques for DLR Script in XAML@ Saturday, November 14, 2009 9:38 PM

Yeah. This is a really, really bad idea. I can't think of one single reason why I would ever do this. Whatever clients are asking for this probably haven't evaluated all the architectural options.

by Rob

# re: Exploring new techniques for DLR Script in XAML@ Saturday, November 14, 2009 10:20 PM

I love this. I understand separation is a good thing, but also understand there are some areas where scripts are handy and useful without breaking the philosophy.

Bindings, triggers, all such are a kind of script which contains logic, but they are good because they are used for presentation, not for business logic. There are cases where presentation logic cannot be achieved with built-in systems and script will save such cases.

I would also love to see it in Binding as well. I think someone on the Internet tried that using JavaScript.

Designers used to write tiny JavaScript to do their presentation work in HTML. Why do we need to take it away in XAML?

We should give them the best tool. And the separation is the matter of the people who uses it.

by Koji

# re: Exploring new techniques for DLR Script in XAML@ Sunday, November 15, 2009 4:16 AM

This should be extremely valuable feature for highly scalable database-oriented solutions! It can save a lot of time and efforts when building xaml-only presentation

# re: Exploring new techniques for DLR Script in XAML@ Sunday, November 15, 2009 1:24 PM

Rob,

This looks very interesting. I am currently working on a porting a project to WPF and Silverlight, which I originally wrote in C++. The whole product is amost entirely scripted the menus, the toolbars and the content.

Being able to execute script directly in the xaml for event handling would probably make life simpler.  My current plan was to use attached properties.

...Stefan

# Links (11/15/2009) &laquo; Steve Pietrek &#8211; Everything SharePoint@ Sunday, November 15, 2009 7:02 PM

Pingback from  Links (11/15/2009) &laquo; Steve Pietrek &#8211; Everything SharePoint

# re: Exploring new techniques for DLR Script in XAML@ Tuesday, September 14, 2010 7:34 AM

Rob, great

Every organization has different policy for handling same thing, we need engine for conditions and actions like games today, for example research.microsoft.com/.../kodu

Milan

# Elegant Code &raquo; WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom@ Tuesday, December 21, 2010 4:37 PM

Pingback from  Elegant Code &raquo; WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom

# WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom &laquo; &lt; web:Blog x:Name=&quot;Brian Lagunas&quot; &#047;&gt;@ Tuesday, December 21, 2010 4:39 PM

Pingback from  WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom &laquo; &lt; web:Blog x:Name=&quot;Brian Lagunas&quot; &#047;&gt;

# WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom &raquo; &lt;xaml:Blog x:Name=&quot;Brian Lagunas&quot; /&gt;@ Wednesday, April 18, 2012 2:21 PM

Pingback from  WPF &#8211; dynamically compile and run event handlers within loose XAML using CodeDom &raquo; &lt;xaml:Blog x:Name=&quot;Brian Lagunas&quot; /&gt;

Leave a Comment

(required) 
(required) 
(optional)
(required)