Embedding Python into Xaml
I took a slightly different tack than Chris on embedding Python into Script. I created an attached property, with the idea that I could set a piece of script "on" an element, giving it some scope. For example, I got this to work:
<
Button Name="Button1" Content="Button1" Grid.Column="0" Grid.Row="1"
Pythalon:PythonScript.Script ="Target.Click += lambda *args:MessageBox.Show('Hello')">
</
Button>
But to do real Python programming, I need to be able to set a multi-line script. Should be easy enough...just use Property Element syntax:
<Button Name="Button1" Content="Button1" Grid.Column="0" Grid.Row="1"
<Pythalon:PythonScript.Script>
def onClick(*args):
MessageBox.Show('Hello Again')
Target.Click += onClick
</Pythalon:PythonScript.Script>
</Button>
However, when I ran this code....I got a Python parsing error. The Xaml parser had removed the new lines in the code, so Python saw something like:
def onClick(*args): MessageBox.Show('Hello Again') Target.Click += onClick
Chris had warned me about this issue, and I knew the solution. Add an xml:space="preserve" attribute.
Unfortunately, the Xaml parser doesn't handle this. I got a message warning me that I couldn't set properties on Property Elements. Oops. I reported the bug...but now I'm kinda stuck. Chris's approach works because he derives from Framework element instead of attaching a property.
Comments
- Anonymous
May 23, 2005
I am excited to announce that the Beta1 Release Candidate (that means all-but-final) build of Avalon...