Professional Programmer Notes

or just call this my soapbox

WPF’ing around with Boo and external xaml files

leave a comment »

Here is a snippet of Boo code (formatted with Python syntax highlighter) that will load a xaml file into a WPF Window:

import System
import System.Windows from PresentationFramework
import System.Windows.Markup from PresentationFramework
import System.IO

class XamlWindow(Window):
	def constructor(name):
		load_xaml(name)

	def load_xaml(name):
		xaml_file = File.OpenRead(Path.GetFullPath(name))
		self.Content = XamlReader.Load(xaml_file)

Application().Run(XamlWindow("mainui.xaml"))</pre>

See http://devpinoy.org/blogs/smash/archive/2006/10/04/XAMl-meets-Boo.aspx for reference.

Alternatively, here is the equivalent IronPython code:

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")

from System.Windows import Window, Application
from System.Windows.Markup import XamlReader
from System.IO import File, Path

class XamlWindow(Window):
	def __init__(self, name):
		self.load_xaml(name)

	def load_xaml(self, name):
		xaml_file = File.OpenRead(Path.GetFullPath(name))
		self.Content = XamlReader.Load(xaml_file)

Application().Run(XamlWindow("mainui.xaml"))

Lastly, here is an example xaml file

<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Label Content="Hello World" />
    <Button Content="Clickable" />
</StackPanel>
Note: There is no UserControl or Window element and the namespaces are defined on the root container, the StackPanel.
Results:
screenshot of very simple wpf app
Advertisement

Written by curtismitchell

May 22, 2011 at 5:39 pm

Posted in .net

Tagged with , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: