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.
- <Page
- xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xml:space='preserve'>
- <Page.Loaded>[<![CDATA[
- tb1.Tag = Car.new('the beach')
- ]]>]</Page.Loaded>
- <Page.LayoutTransform>
- <ScaleTransform ScaleX='3' ScaleY='3' />
- </Page.LayoutTransform>
- <UniformGrid Columns='2' Rows='4'>
- <Button Click='[tb1.Text=tb1.Tag.go()]'>go</Button>
- <Button Click='[tb1.Text=tb1.Tag.stop()]'>stop</Button>
- <Label Target='tb1' >tb1:</Label>
- <TextBox Name='tb1' />
- <Label Target='tb2'>tb2:</Label>
- <TextBox Name='tb2' />
- <Label Target='tb3'>tb3:</Label>
- <TextBox Name='tb3' />
- </UniformGrid>
-
-
- <x:Code><![CDATA[
- class Car < Object
- attr_accessor :destination
- def initialize(destination)
- @destination = destination
- end
- def go()
- 'starting'
- end
- def stop()
- 'stopping'
- end
- end
- ]]></x:Code>
- </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/)